所以我有一个 JSON 就像下图存储为 MYJSON
--
我的计划是从 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
是动态的,计数会不时变化。
谢谢
最佳答案
由于 childObject0
中的每个对象是 json,你可以将它存储为 ArrayList<JSONObject>
.这应该使对象比作为字符串更容易处理。
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();
}
关于java - 从 JSON 对象创建 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382291/