0

我有一个 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"

    }
    ]
}

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

4

3 回答 3

2

尝试 "-url""media:content"当前的 json 字符串中获取:

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                                   
 }
于 2012-12-22T12:04:32.903 回答
2

编写下面的代码来解析-url字符串,它将解决您的问题。

JSONObject mMainJsonObj = new JSONObject("Pass Json Response String Here");
JSONArray  mItemJsonArray = mMainJsonObj.getJSONArray("item");

for (int i = 0; i < mItemJsonArray.length(); i++) {
    JSONObject mJsonObj1 = mItemJsonArray.getJSONObject(i);
    String mTitle = mJsonObj1.getString("title");
    String mLink = mJsonObj1.getString("link");

    JSONObject mJsonObjGuid = mJsonObj1.getJSONObject("guid");
    String mIsPermLink = mJsonObjGuid.getString("-isPermaLink");
    String mText = mJsonObjGuid.getString("#text");

    JSONObject mJsonObjAtomLink = mJsonObj1.getJSONObject("atom:link");
    String mRel = mJsonObjAtomLink.getString("-rel");
    String mHref = mJsonObjAtomLink.getString("-href");

    JSONObject mJsonObjMediaContent = mJsonObj1.getJSONObject("media:content");
    String mUrl = mJsonObjMediaContent.getString("-url");
    String mMedium = mJsonObjMediaContent.getString("-medium");
    String mHeight = mJsonObjMediaContent.getString("-height");
    String mWidth = mJsonObjMediaContent.getString("-width");
}

有关更多信息,请参见下面的链接。

Json 解析示例

于 2012-12-22T12:55:09.343 回答
1

JsonNode杰克逊的解决方案:使用 an将您的 JSON 读入ObjectMapper并检索您的值,如下所示:

// Since JsonNode implements Iterable of itself and cycles through array elements,
// this works
for (final JsonNode element: node)
    doSomethingWith(element.get("media:content").get("-url"));
于 2012-12-22T12:41:20.327 回答