【问题标题】:how to get data from inner json array in android如何从android中的内部json数组获取数据
【发布时间】:2017-01-22 21:51:30
【问题描述】:

请我尝试从 JSON 数组 thumbnail_images 中获取 URL,它位于另一个 medium_large 数组中,我如何从第二个数组中获取数据。我已经能够从这个 JSON 中获取 URL

"attachments": [

{
    "id": 367,
    "url": "http://street2view.com/wp-content/uploads/2017/01/mmm.png",

使用此代码:

JSONArray attachments = post.getJSONArray("attachments");
if (null != attachments && attachments.length() > 0) {
   JSONObject attachment = attachments.getJSONObject(0);
   if (attachment != null)
      item.setAttachmentUrl(attachment.getString("url"));
}

那么我如何从内部数组中获取数据,如这段代码中的我如何获取 URL

"thumbnail_images": {
    "medium_large": {
        "url": "http://street2view.com/wp-content/uploads/2017/01/mmm.png",
        "width": 749,
        "height": 400

【问题讨论】:

    标签: java android arrays json


    【解决方案1】:

    这是基于您的示例,该示例没有“thumbnail_images”作为数组,而只是一个属性

    JSONObject images = post.getJSONObject("thumbnail_images");
    JSONObject mediumLarge = images.getJSONObject("medium_large");
    String url = mediumLarge.optString("url");
    

    【讨论】:

      【解决方案2】:

      我知道了!

                  JSONObject images = post.getJSONObject("thumbnail_images");
                  JSONObject mediumLarge = images.getJSONObject("medium_large");
                  item.setAttachmentUrl(mediumLarge.optString("url"));
      

      感谢@classicalConditioning 的提示。

      【讨论】:

      • 如果有帮助,请务必接受@classicalConditioning 的回答。
      猜你喜欢
      • 1970-01-01
      • 2020-08-18
      • 2021-11-04
      • 2015-02-02
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多