java - 解析 JSON 对象中的 JSON 对象

标签 java android json

我有一个 JSON 文件,其中包含项目对象数组:

{
  "item": [
    {
      "title": "TitleA",
      "link": "http://www.abc.html?partner=rss&emc=rss",
      "guid": {
        "-isPermaLink": "true",
        "#text": "www.abc.html"
      },
      "atom:link": {
        "-rel": "standout",
        "-href": "http://www.abc.html?partner=rss&emc=rss"
      },
      "media:content": {
        "-url": "standard.jpg",
        "-medium": "image",
        "-height": "75",
        "-width": "75"
      },
      "media:description": "This is the description.",
      "media:credit": "Reuters",
      "description": "In depth description",
      "dc:creator": "By test creator",
      "pubDate": "Sun, 21 Oct 2012 11:29:12 GMT",
      "category": "World"

    },
    {
      "title": "TitleB",
      "link": "http://www.abc.html?partner=rss&emc=rss",
      "guid": {
        "-isPermaLink": "true",
        "#text": "www.abc.html"
      },
      "atom:link": {
        "-rel": "standout",
        "-href": "http://www.abc.html?partner=rss&emc=rss"
      },
      "media:content": {
        "-url": "standard.jpg",
        "-medium": "image",
        "-height": "75",
        "-width": "75"
      },
      "media:description": "This is the description.",
      "media:credit": "Reuters",
      "description": "In depth description",
      "dc:creator": "By test creator",
      "pubDate": "Sun, 21 Oct 2012 11:29:12 GMT",
      "category": "World"

    }
    ]
}

现在我知道如何获取“标题”,但我不知道如何访问“media:content”中的“-url”,因为它似乎是 Item 对象中的 JSON 对象。我如何获取该值并将其分配给我的 Item 类中的值?

最佳答案

尝试从当前json字符串中获取“media:content”内的“-url”:

JSONObject jsonObject = new JSONObject("Your JSON STRING HERE");

JSONArray  jsonArray =jsonObject.getJSONArray("item");

 for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObjectitem=
                           jsonArray.getJSONObject(i);                                                                            

  // get title or link here
     String strtitle=jsonObjectitem.getString("title");
      //....get other values in same way 

   // get media:content json object
  JSONObject jsonObjectmediacontent = 
                       jsonObjectitem.getJSONObject("media:content");

   // get url,medium,...

     String strurl=jsonObjectmediacontent.getString("-url"); 
     //....get other values in same way                                   
 }

关于java - 解析 JSON 对象中的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14002564/

相关文章:

java - 在JavaFX中,有没有办法先显示选项卡名称,然后显示图形?

java - 如何在 JSP 中获取 NullPointerException 的详细信息?

java - java中如何通过反射调用setter方法

javascript - 在本地存储中保存一个函数

json - 在 AJAX 响应文本中使用 jQuery 和 JSON?

java - 带有嵌套 Java 和 Xml Spring 配置的 JUnit

Android:如何使用非字符串选择参数查询 SQLiteDatabase?

android - 拦截RecyclerView在NestedScrollView中向下滑动

android - mvvm android 中的存储库可以是单例吗?

javascript - Json 字符串未显示在我的 jsp 页面上