简体   繁体   中英

Creating an ArrayList from a JSON Object

So I have a JSON just like the picture below stored as MYJSON --

在此处输入图片说明

My plan is to retrieve all the object from childObject0 and store them in an ArrayList so they can be processed later. So I did-

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();
        }

Can't figure out how to append. Is this right approach? childObject0 is dynamic and the count changes time to time. Thanks

Since each object in childObject0 is json, you can store it as an ArrayList<JSONObject> . That should make the objects easier to process than as 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM