1

Within this Json response, how do I get access to "smallImageUrls" and get the image url using Java?

Seems like "smallImageUrls" is an array within the "matches" jsonarray. Someone please correct me if I am wrong on that.

{  
   "attribution":{  
      "html":"Recipe search powered by <a href='http://www.yummly.com/recipes'><img alt='Yummly' src='http://static.yummly.com/api-logo.png'/></a>",
  "url":"http://www.yummly.com/recipes/",
  "text":"Recipe search powered by Yummly",
  "logo":"http://static.yummly.com/api-logo.png"
   },
"totalMatchCount":17663,
"facetCounts":{  

},
"matches":[  
  {  
     "imageUrlsBySize":{  
        "90":"http://lh3.ggpht.com/bTkxROvVTjHChEsGLRnkuwPoi-eNrHmESYP3xDHMsIisN-U06z-OfwErSjT5AHvMG0Ccgw8cN4mVqNyjWzbz=s90-c"
     },
     "sourceDisplayName":"Serious Eats",
     "ingredients":[  
        "mayonnaise",
        "crema mexican",
        "feta",
        "ancho powder",
        "garlic",
        "coriander leaf",
        "shuck corn",
        "lime"
     ],
     "id":"Mexican-street-corn-_elotes_-370469",
     "smallImageUrls":[  
        "http://lh5.ggpht.com/itong2VhnBU2mvPtzNimL58MnkC4l113RgNyrEWq8Jf76AsOGOlBoVQyCF-jYDPtzTB-7SoViNzyV5-Xe0NS=s90"
     ],
     "recipeName":"Mexican Street Corn (Elotes)",
     "totalTimeInSeconds":2400,
     "attributes":{  
        "cuisine":[  
           "Mexican"
        ]
     },
     "flavors":{  
        "sweet":0.5,
        "sour":0.6666666666666666,
        "salty":0.6666666666666666,
        "piquant":0.3333333333333333,
        "meaty":0.3333333333333333,
        "bitter":0.6666666666666666
     },
     "rating":5
  }
],
"criteria":{  
  "excludedIngredients":null,
  "allowedIngredients":null,
  "terms":null
}
 }

this is the code that I currently have. I can access all the other strings, just not the image urls .

 JSONObject resObj = new JSONObject(result);
                 JSONArray foundrecipes = resObj.getJSONArray("matches");
                for(int i = 0;i<foundrecipes.length(); i++){
                    JSONObject recipe = foundrecipes.getJSONObject(i);
                    String recipeName = recipe.getString("recipeName");
                     String rating = recipe.getString("rating");
                    String id = recipe.getString("id");
                    String imageurl = recipe.getString("smallImageUrls");

                data.add(new Recipes(recipeName, rating, id, imageurl));
            }
1
  • 1
    good tool to visualise Json strings and help you out decoding them: jsonviewer.stack.hu Commented Sep 17, 2014 at 15:08

1 Answer 1

1

smallImageUrls, is inside matches that is inside the root, so you have to retrieve matches and, through it, smallImageUrls

JSONObject obj = new JSONObject(...);
JSONArray matches = obj.optJSONArray("matches");
if (matches != null) {
 for (int i = 0; i < matchesLenght; i++) {
    JSONObject objAtIndex =  matches.optJSONObject(i);
    if (objAtIndex != null) {
         JSONArray smallImageUrls = objAtIndex.optJSONArray("smallImageUrls");
         for (int j = 0; j < smallImageUrlsSize; j++) {
              String urlAtIndex = smallImageUrls.optString(j);
         }
    }
 }

}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much! within the JSONArray smallImageUrls, how do i get the first image url and convert it to string?
I edited my answer. The url is already a string. You have to loop in the smallImageUrls array

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.