在Response中解析json数组

时间:2016-01-30 10:18:24

标签: android

我似乎无法理解如何解析“dispay_sizes”JSON数组。 到目前为止,这是我的方法。

这是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"
        },

我的解析代码:

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

有人可以解释如何在JSON数组中解析JSON数组吗?

1 个答案:

答案 0 :(得分:1)

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

是对的

  [  // 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"

你有json对象的数组。

所以你需要

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