0

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.

Jayson Tamayo
  • 2,741
  • 3
  • 49
  • 76

3 Answers3

1
JSONObject jSONObject = new JSONObject(yourStrinig);
JSONArray jArray = jSONObject.getJSONArray("Apps");
for (int i = 0; i < jArray.length(); i++) {
    JSONObject childrenObject = jArray.getJSONObject(i);
    String column1 = childrenObject.getString("column1");
    String column2 = childrenObject.getString("column2");
}
Miral Bhalani
  • 274
  • 2
  • 9
0

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

Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
0

with this you can loop through all elements

private void showJSON(String response){
                String name="";
                String phone_number="";
                try {
                    JSONObject jsonObject =new JSONObject(response);
                    JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
                    for(int x=0;x<result.length();x++) {
                        JSONObject collegeData = result.getJSONObject(x);
                        name = collegeData.getString(config.KEY_NAME);
                        phone_number = collegeData.getString(config.KEY_PHONE_NUMBER);
                       //you can save your data in list here
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
Sarmad Shah
  • 3,725
  • 1
  • 20
  • 42