代碼如下:
JSONObject jChild=new JSONObject();
JSONObject jParent=new JSONObject();
for (Product p : boxAdapter.getBox()) {
if (p.checked){
try {
jChild.put("uid", p.uid);
list.add(String.valueOf(jChild));
//list.add(String.valueOf(jParent));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
jParent.put("users", list);
// Toast.makeText(this, ""+jParent, Toast.LENGTH_LONG).show();
Log.v("TakeAttendance","JSONpARENT "+String.valueOf(jParent));
輸出:
{"users":"[{\"uid\":\"4\"}, {\"uid\":\"5\"}, {\"uid\":\"6\"}]"}
我所需要的是:
{users: [
{
name: "acx",
uid: "2"
},
{
name: "test",
uid: "6"
},
{
name: "ccc",
uid: "11"
}
]
}
private void jsonTest() {
try {
JSONObject json = new JSONObject();
JSONArray jsonArr = new JSONArray();
for (int i = 0; i < 3; i++) {
JSONObject arrObj = new JSONObject();
arrObj.put("name", "test");
arrObj.put("uid", i);
jsonArr.put(arrObj);
}
json.put("users", jsonArr);
Log.e("LH", "json==" + json);
} catch (JSONException e) {
e.printStackTrace();
}
}