从JSON对象创建ArrayList

时间:2016-07-14 19:04:46

标签: java android json arraylist

所以我有一个JSON,就像下面存储为MYJSON -

的图片一样

enter image description here

我的计划是从childObject0检索所有对象并将其存储在ArrayList中,以便稍后处理。所以我做了 -

ArrayList<String> childLists = new ArrayList<String>();
 try {
       JSONArray childArray = MYJSON.getJSONArray("childObject0");
       for (int i = 0; i<jArray.length(); i++
                 ) {
           //I lost it here! How can I append to `childList` from `childArray`?

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

无法弄清楚如何追加。这是正确的方法吗? childObject0是动态的,计数会不时变化。 感谢

1 个答案:

答案 0 :(得分:2)

由于childObject0中的每个对象都是json,因此您可以将其存储为ArrayList<JSONObject>。这应该使对象比Strings更容易处理。

ArrayList<JSONObject> childList = new ArrayList<JSONObject>();
try {
    JSONArray childArray = MYJSON.getJSONArray("childObject0");
    for (int i = 0; i < childArray.length(); i++) {
        childList.add(childArray.getJSONObject(i));
    }
} catch (JSONException e) {
    e.printStackTrace();
}