Android json没有给我任何格式,
我正在制作Json,但没有制作正确的格式 它只给我用户的第一个属性和最后一个属性项,没有给我完整的json。。 提前谢谢。。 我的代码是Android json没有给我任何格式,,android,json,Android,Json,我正在制作Json,但没有制作正确的格式 它只给我用户的第一个属性和最后一个属性项,没有给我完整的json。。 提前谢谢。。 我的代码是 JSONObject json = new JSONObject(); try { JSONObject subJson = new JSONObject(); //JSONArray jsonArray = new JSONArray(); subJson.put("
JSONObject json = new JSONObject();
try {
JSONObject subJson = new JSONObject();
//JSONArray jsonArray = new JSONArray();
subJson.put("saleManId", LogIn.UserId);
subJson.put("customerId", LogIn.CustomerId);
json.put("User", subJson);
for (int i = 0; i < LogIn.subValues.length; i++) {
JSONObject subSubObj = new JSONObject();
for (int j = 0; j < LogIn.subValues[i].length; j++) {
if (LogIn.subValues[i][j] > 0) {
JSONObject item = new JSONObject();
item.put("itemID", LogIn.subIds[i][j]);
item.put("qty", LogIn.subValues[i][j]);
subSubObj.put("item", item);
}
}
if(subSubObj.length()>0)
{
json.put("orderDetail", subSubObj);
Log.d("json", json.toString());
}
}
Log.d("jsonnnnnnnnnnnnnnnnnnnnnnnnnnnnn", json.toString());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我明白了……:)谢谢大家
JSONObject json = new JSONObject();
try {
JSONObject subJson = new JSONObject();
JSONArray jsonArray = new JSONArray();
subJson.put("saleManId", LogIn.UserId);
subJson.put("customerId", LogIn.CustomerId);
json.put("User", subJson);
for (int i = 0; i < LogIn.subValues.length; i++) {
for (int j = 0; j < LogIn.subValues[i].length; j++) {
if (LogIn.subValues[i][j] > 0) {
JSONObject subSubObj = new JSONObject();
JSONObject item = new JSONObject();
item.put("itemID", LogIn.subIds[i][j]);
item.put("qty", LogIn.subValues[i][j]);
subSubObj.put("item", item);
jsonArray.put(subSubObj);
}
}
}
json.put("orderDetail", jsonArray);
Log.d("jsonnnnnnnnnnnnnnnnnnnnnnnnnnnnn", json.toString());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sendString = json.toString();
如果你想要一个objects集合,你需要一个JSONArray,但是我也需要key。,一个jsonobject对于一个给定的key只能有一个值。。。
JSONObject json = new JSONObject();
try {
JSONObject subJson = new JSONObject();
JSONArray jsonArray = new JSONArray();
subJson.put("saleManId", LogIn.UserId);
subJson.put("customerId", LogIn.CustomerId);
json.put("User", subJson);
for (int i = 0; i < LogIn.subValues.length; i++) {
for (int j = 0; j < LogIn.subValues[i].length; j++) {
if (LogIn.subValues[i][j] > 0) {
JSONObject subSubObj = new JSONObject();
JSONObject item = new JSONObject();
item.put("itemID", LogIn.subIds[i][j]);
item.put("qty", LogIn.subValues[i][j]);
subSubObj.put("item", item);
jsonArray.put(subSubObj);
}
}
}
json.put("orderDetail", jsonArray);
Log.d("jsonnnnnnnnnnnnnnnnnnnnnnnnnnnnn", json.toString());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sendString = json.toString();
{"orderDetail":[
{"item":{"qty":1,"itemID":"4"}},
{"item":{"qty":1,"itemID":"14"}},
{"item":{"qty":1,"itemID":"28"}},
{"item":{"qty":1,"itemID":"59"}},
{"item":{"qty":1,"itemID":"60"}},
{"item":{"qty":1,"itemID":"62"}},
{"item":{"qty":1,"itemID":"24"}},
{"item":{"qty":1,"itemID":"36"}},
{"item":{"qty":1,"itemID":"51"}}],
"User":{"customerId":"414","saleManId":"1"}}