Jayson Tamayo Jayson Tamayo - 1 month ago 20
Android Question

How to loop through JSONArray inside JSONObject

I currently have the following JSON structure:

{
"Apps": [
{
"column1": "sample string 1",
"column2": true
},
{
"column1": "sample string 1",
"column2": true
}
],
"param": true
}


How do I get the values of the column1 and column2? What I only know how to parse is a JSONObject within a JSONArray.

Answer

Something like this.

// Get a reference to the JSON object
JSONObject jSONObject = new JSONObject(stringJsonResponse);
// Getting the JSON array node
JSONArray jsonAray = jSONObject.getJSONArray("Apps");
// Looping through the json array
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject childrenObject = childrenArray.getJSONObject(i);
    ...
    ...
    ...
}

You can take a look at how I parsed JSON data when I received similar data https://github.com/gSrikar/AskReddit/blob/master/app/src/main/java/com/example/srikar/askreddit/MainActivity.java