如何使JSONArray为空或删除它

时间:2012-04-09 09:02:01

标签: android json

完成活动的执行后,我想让jSONArray为空或删除它。因为当我反复调用活动时,新数据会附加到JSONArray中的旧数据。我的jsonArray如下所示..

[{"record":[
            {......},
            {.......}
           ]
 },
 {"record":[
            {..........},
            {.........}
           ]
 }
] 


First time:
[{"record":[{"intensity":"Low","Body_SubParts":"Scalp","symptom":"Bleeding"}]}]

Second time:
[{"record":[{"intensity":"Low","Body_SubParts":"Scalp","symptom":"Bleeding"},
{"intensity":"Low","Body_SubParts":"Eyes","symptom":"Bleeding"}]}]

2 个答案:

答案 0 :(得分:0)

已回答here

// Assuming you have constructed the replacement as
JSONObject jsonArray11_obj2 = new JSONObject();
jsonArray11_obj2.put("intensity", "high");
jsonArray11_obj2.put("Body_SubParts", "Scalp");
// ...

// Then the code doing the replacement would look similar to
int index = 0;
((JSONObject)jsonArray.get(index)).getJSONArray("record").put(0, jsonArray11_obj2);

答案 1 :(得分:0)

构建JSON数组后,使JSONArray对象无效。下次再次构建JSON数组