Veeresh Charantimath
Veeresh Charantimath

Reputation: 15

Parse json array in Response

I can't seem to understand how to parse the "dispay_sizes" JSON array. Here is my approach so far.

This is the JSON

{
"result_count": 6577719,
"images": [
  {
            "id": "505093872",
            "asset_family": "creative",
            "caption": "Professional footballer kicking ball during game in floodlit soccer stadium full of spectators under stormy sky and floodlights. It's snowing up there.",
            "collection_code": "EPL",
            "collection_id": 712,
            "collection_name": "E+",
            "display_sizes": 
    [{
                "is_watermarked": false,
                "name": "thumb",
                "uri": "http://cache4.asset-cache.net/xt/505093872.jpg?v=1&g=fs1|0|EPL|93|872&s=1&b=RjI4"
            }],
            "license_model": "royaltyfree",
            "max_dimensions": {
                "height": 6888,
                "width": 8918
            },
            "title": "Soccer player kicking a ball"
        },

My code to parse it:

 JSONObject imageData = new JSONObject(jsonData);
    int resultCount = imageData.optInt("result_count");
    Log.d("Result Count: ",Integer.toString(resultCount));

    JSONArray imageArray = imageData.optJSONArray("images");
    for( int i = 0; i < imageArray.length(); i++)
    {
        JSONObject img= imageArray.getJSONObject(i);
        String id = img.optString("id");
        allID.add(id);
        Log.d("Image ID: ", id);
        JSONArray imagePath = img.getJSONArray("display_sizes");
        String pathURI = imagePath.getString("uri");
        Log.d("Image Path: ",pathURI);
    }

Can someone explain how to parse JSON array within a JSON array?

Upvotes: 0

Views: 104

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

This

JSONArray imagePath = img.getJSONArray("display_sizes"); 

is right

  [  // represents json array
    { // jsonobject
            "is_watermarked": false,
            "name": "thumb",
            "uri": "http://cache4.asset-cache.net/xt/505093872.jpg?v=1&g=fs1|0|EPL|93|872&s=1&b=RjI4"

you have array of json object.

So you need

for(int j=0;j<imagePath.length();j++)
{
JSONObject jb = imagePath.getJSONObject(j);
String pathURI = jb.getString("uri");
}

Upvotes: 1

Related Questions