简体   繁体   English

如何在JSON对象响应中访问JSONArray内部的JSONArray

[英]How to access JSONArray inside of a JSONArray in a JSON object response

Within this Json response, how do I get access to "smallImageUrls" and get the image url using Java? 在此Json响应中,如何使用Java访问“ smallImageUrls”并获取图像URL?

Seems like "smallImageUrls" is an array within the "matches" jsonarray. 似乎“ smallImageUrls”是“ 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 . 我可以访问所有其他字符串,而不能访问图像url。

 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));
            }

smallImageUrls, is inside matches that is inside the root, so you have to retrieve matches and, through it, smallImageUrls smallImageUrls位于根目录内的匹配项中,因此您必须检索匹配项,并通过它来检索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);
         }
    }
 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM