0

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?

4
  • Which JSON library do you use? Commented Dec 22, 2012 at 11:49
  • Fyi, the json you posted lacks a ] at the end and contains a dangling , that shouldn't be there. Might be just a copy&paste mistake though... Commented Dec 22, 2012 at 11:52
  • Yep, just a copying error because the file contains more items, thanks Commented Dec 22, 2012 at 11:56
  • just try this androidhive.info/2012/01/android-json-parsing-tutorial Commented Dec 22, 2012 at 12:45

3 Answers 3

2

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                                   
 }
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

Your answer is the most complete, however ρяσѕρєя K posted a similar answer before you, so I will have to accept his. Thanks for the help, +1 upvote
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.