【问题标题】:Get default url from this json? [duplicate]从此 json 获取默认 url? [复制]
【发布时间】:2018-07-08 14:04:20
【问题描述】:
     JSONObject jsonObject = new JSONObject(content);
            JSONArray jsonArray =  jsonObject.getJSONArray("message");
            for(int i =0;i<jsonArray.length(); i++){
                JSONObject productObject = jsonArray.getJSONObject(i);

                arrayList.add(new Product(
                       productObject.getString("url"),
                        productObject.getString("title"),
                        productObject.getString("description")
                ));

    {  
   "message":[  
      {  
         "id":"hT_nvWreIhg",
         "link":"https://www.youtube.com/watch?v=hT_nvWreIhg",
         "kind":"youtube#video",
         "publishedAt":"2013-05-31T07:00:36.000Z",
         "channelId":"UCQ5kHOKpF3-1_UCKaqXARRg",
         "channelTitle":"OneRepublicVEVO",
         "title":"OneRepublic - Counting Stars",
         "description":"Get OneRepublic's new album 'Oh My My,' featuring 'Kids,' 'Wherever I Go' and 'Let's Hurt Tonight:' http://smarturl.it/OhMyMy Listen to the Best of OneRepublic ...",
         "thumbnails":{  
            "default":{  
               "url":"https://i.ytimg.com/vi/hT_nvWreIhg/default.jpg",
               "width":120,
               "height":90
            },
            "medium":{  
               "url":"https://i.ytimg.com/vi/hT_nvWreIhg/mqdefault.jpg",
               "width":320,
               "height":180
            },
            "high":{  
               "url":"https://i.ytimg.com/vi/hT_nvWreIhg/hqdefault.jpg",
               "width":480,
               "height":360
            }
         }
      }
   ]
}

【问题讨论】:

  • 您为什么期望productObject.getString("url") 会返回任何东西?这并不是说你想要 哪个 url

标签: java android json parsing


【解决方案1】:

您还需要两次致电getJSONObject

JSONObject defaultObject = productObject.getJSONObject("thumbnails").getJSONObject("default")

然后使用

arrayList.add(new Product(
    defaultObject.getString("url"),
    productObject.getString("title"),
    productObject.getString("description")
));

【讨论】:

  • 认为人,但它不起作用,我只是在获取 url 以在 listView 中显示图像时遇到问题,标题和描述都可以
  • @youssefhrizi cricket_007 修复了答案中的错误,请重试。
  • 它工作,很想你
猜你喜欢
  • 2016-06-08
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 2016-05-05
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
相关资源
最近更新 更多