【问题标题】:Having trouble Parsin a JSON Array in Android Studio在 Android Studio 中解析 JSON 数组时遇到问题
【发布时间】:2016-09-23 17:58:07
【问题描述】:

我知道解析 JSON 的方法,但是当我尝试访问我想要的值是 Android Studio 的键时,我会收到以下消息:

正如你所看到的,“大”键没有任何价值,但显然它是,无论如何,这是我的 json 解析方法

 public static List<News> parseJSONtoNews(JSONArray jsonArray) throws JSONException {
    List<News> newsList = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {
        News news = new News();
        JSONObject jsonObjectNews = jsonArray.getJSONObject(i);

        JSONArray jsonArrayCategories = jsonObjectNews.getJSONArray(JSONKeys.KEY_CATEGORIES);


        int category = getCatgories(jsonArrayCategories);
        news.setCategories(category);

        news.setiD(jsonObjectNews.getInt("id"));

        JSONObject jsonObjectTitle = jsonObjectNews.getJSONObject("title");
        news.setTitle(jsonObjectTitle.getString("rendered"));

        JSONObject jsonObjectContent = jsonObjectNews.getJSONObject("content");
        news.setContent(jsonObjectContent.getString("rendered"));

        JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_image");
        JSONObject jsonObjectMediaDetails = jsonObjectImage.getJSONObject("media_details");
        JSONObject jsonObjectSizes = jsonObjectMediaDetails.getJSONObject("sizes");
        JSONObject jsonObjectMediumLarge = jsonObjectSizes.getJSONObject("large");
        news.setImageURL(jsonObjectMediumLarge.getString("source_url"));
        newsList.add(news);
    }



    return newsList;
}

public static int getCatgories(JSONArray jsonArray) throws JSONException{
    int categories = 0;
    for (int i = 0; i<jsonArray.length(); i++){
        categories = jsonArray.getInt(i);
    }

    return categories;
}

这是我要解析的 JSON

[
  {
    "id": 742,
        "title": {
      "rendered": “title”
    },
    "content": {
      "rendered": “content”
    },
    "categories": [
      4
    ],
    "tags": [],
    "better_featured_image": {
        "file": "2016/05/20160520_191324.jpg",
        "sizes": {
          "thumbnail": {
            "file": "20160520_191324-150x150.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-150x150.jpg"
          },
          "medium": {
            "file": "20160520_191324-300x169.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-300x169.jpg"
          },
          "medium_large": {
            "file": "20160520_191324-768x432.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-768x432.jpg"
          },
          "large": {
            "file": "20160520_191324-1024x576.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-1024x576.jpg"
          }
        },
      "post": 742,
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324.jpg"
    },
 },
{
    "id": 745,
        "title": {
      "rendered": “title”
    },
    "content": {
      "rendered": “content”
    },
    "categories": [
      4
    ],
    "tags": [],
    "better_featured_image": {
        "file": "2016/05/20160520_191324.jpg",
        "sizes": {
          "thumbnail": {
            "file": "20160520_191324-150x150.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-150x150.jpg"
          },
          "medium": {
            "file": "20160520_191324-300x169.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-300x169.jpg"
          },
          "medium_large": {
            "file": "20160520_191324-768x432.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-768x432.jpg"
          },
          "large": {
            "file": "20160520_191324-1024x576.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-1024x576.jpg"
          }
        },
      "post": 742,
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324.jpg"
    },
   }
]

如您所见,存在“大”键但我无法访问它,它与“缩略图”键一起使用,所以我不知道如何处理这个,希望你们能帮助我! 谢谢

【问题讨论】:

标签: android arrays json parsing


【解决方案1】:

在我看来:

JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_media");
JSONObject jsonObjectMediaDetails = jsonObjectImage.getJSONObject("media_details");

jsonObjectMediaDetails 不应该存在。我在 JSON 中的任何地方都看不到 media_details。我也没有看到better_featured_media。我觉得应该是better_featured_image

试试:

JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_image");
JSONObject jsonObjectSizes = jsonObjectImage.getJSONObject("sizes");

【讨论】:

  • 很抱歉,我在一个名为 JSONKEYS 的类中拥有所有这些键,我写错了,但在我的代码中它显示“better_featured_image”,正如我告诉你的那样,我可以访问 htumbnail 键但我不能到大的,(这是我想要的)谢谢你的回复,我很感激
  • @miguelacio 在我看来,您仍然需要拨打一个您不需要的额外电话:getJsonObject("media_details")。您发布的 JSON 中不存在该密钥。尝试在该函数中添加断点,并单步执行每个语句并检查每个步骤的值。
  • @miguelacio 我认为 Kevin 是对的,我也观察到了代码并且“media_details”键不存在。删除那个并尝试不使用它。
  • 米格尔请试试这个。它必须是正确的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
相关资源
最近更新 更多