Matt
Matt

Reputation: 3970

Parsing a JSON object within a JSON object

I have a JSON file which contains an array of item objects:

{
  "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"

    }
    ]
}

Now I know how to get the "title", but I don't know how I would access the "-url" within "media:content" for example, since it seems to be a JSON object within the Item object. How would I get this value and assign it to a value in my Item class?

Upvotes: 0

Views: 1168

Answers (3)

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Write below code to parse -url string, it will solve your problem.

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");
}

And see below link for more information.

Json Parsing Example

Upvotes: 2

fge
fge

Reputation: 121830

Solution with Jackson: read your JSON into a JsonNode using an ObjectMapper and retrieve your values like this:

// 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"));

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

try as to get "-url" within "media:content" from current json string :

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                                   
 }

Upvotes: 2

Related Questions