0

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?

2
  • What happens with the above code? Commented Jan 30, 2016 at 10:20
  • I can get till the "id" Object but cannot parse the "display_sizes" Commented Jan 30, 2016 at 10:22

1 Answer 1

1

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");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.