0

I am trying to access the next level of my JSON result. However it keeps saying that there is not value for the mediaGroup element. I have only managed to the other elements on the same level such as title, contentSnippet and publishedDate

This is part of the json result

{
"responseData": {
    "feed": {
        "feedUrl": "http://www.abc.net.au/news/feed/51120/rss.xml",
        "title": "Just In",
        "link": "http://www.abc.net.au/news/justin/",
        "author": "",
        "description": "",
        "type": "rss20",
        "entries": [
            {
                "mediaGroups": [
                    {
                        "contents": [
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-16x9-2150x1210.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "height": 1210,
                                "width": 2150,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            },
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-4x3-940x705.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "height": 705,
                                "width": 940,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            },
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-3x2-940x627.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "isDefault": "true",
                                "height": 627,
                                "width": 940,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            },
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-3x4-940x1253.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "height": 1253,
                                "width": 940,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            },
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-1x1-1400x1400.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "height": 1400,
                                "width": 1400,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            }
                        ]
                    }
                ],
                "title": "New proposed tyre shredding facility receives EPA approval",
                "link": "http://www.abc.net.au/news/2016-05-28/tyre-shredding-facility-proposed-tasmania-south-epa-approval/7456326",
                "author": "",
                "publishedDate": "Fri, 27 May 2016 23:42:46 -0700",
                "contentSnippet": "A long-term solution to Tasmania's tyre waste problem is on the horizon, with a new facility preparing to shred thousands of ...",
                "content": "<p>A long-term solution to Tasmania's tyre waste problem is on the horizon, with a new facility preparing to shred thousands of tyres every year.</p>",
                "categories": [
                    "Environmental Impact",
                    "Environment",
                    "Government and Politics"
                ]

This is my code to parse it out

public List<NewsObj> constructJSON(String jsonIN){
    ArrayList<NewsObj> newsList = new ArrayList<>();
    try{
        //add more levels to extract json
        JSONObject jsonObject1 = new JSONObject(jsonIN);
        String responseData = jsonObject1.getString("responseData");
        Log.d("RECEIVEJSONOBJECTLEVEL1",responseData);

        JSONObject jsonObject2 = new JSONObject(responseData);
        String feed = jsonObject2.getString("feed");
        Log.d("RECEIVEJSONOBJECTLEVEL2",feed);

        JSONObject jsonObject3 = new JSONObject(feed);
        String entries = jsonObject3.getString("entries");
        Log.d("RECEIVEJSONOBJECTLEVEL3",entries); //this opens up further thumbnail sizes



        JSONArray jsonArray1 = new JSONArray(entries);
        for(int i=0; i<jsonArray1.length();i++){
            JSONObject mediaGroups = jsonArray1.getJSONObject(i);
            JSONArray jsonArray2 = mediaGroups.getJSONArray("mediaGroups");
            String title = mediaGroups.getString("title");
            String url = mediaGroups.getString("link");
            String description = mediaGroups.getString("contentSnippet");
            String publishedDate = mediaGroups.getString("publishedDate");
//                main information for news article
            Log.d("RECEIVEJSONOBJECTLEVEL4",title);
            Log.d("RECEIVEJSONOBJECTLEVEL4",url);
            Log.d("RECEIVEJSONOBJECTLEVEL4",description);
            Log.d("RECEIVEJSONOBJECTLEVEL4",publishedDate);
            NewsObj aObj = new NewsObj(title,url,publishedDate);
            newsList.add(aObj);

I can't seem to find any value from mediaGroup. Please help

JianYA
  • 2,750
  • 8
  • 60
  • 136
  • 1
    mediaGroups is an array too and also contents – Anoop Kanyan May 28 '16 at 12:28
  • Possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Nongthonbam Tonthoi May 28 '16 at 12:32
  • Sorry I can't seem to get it still. Could you add an answer? – JianYA May 28 '16 at 12:33
  • Mine is asking for how to access the mediaGroup element as I can't seem to get it even if I added it to a JSONArray – JianYA May 28 '16 at 12:33
  • 1
    I imagine the response is too large to post completely. I recommend you use this site to get a cleaner overview of your JSON. It's a bit easier to see what's an array & what is an object thanks a cleaner presentation. Also you can "chain" your call so you could say `JSONObject jsonObject1 = new JSONObject(jsonIN);` then `jsonObject1.getJSONObject("responseData").getJSONObject("feed").getJSONArray("e‌​ntries").` – Daniel May 28 '16 at 12:40
  • I updated my code but am still getting the the same exception – JianYA May 28 '16 at 12:41
  • check if jsonArray1 is not null – Anoop Kanyan May 28 '16 at 12:47
  • Can you provide a link to the raw JSON? – Daniel May 28 '16 at 12:48
  • Yup its not. The other variables have items in it – JianYA May 28 '16 at 12:48
  • http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=25&q=http://www.abc.net.au/news/feed/51120/rss.xml Raw JSON link – JianYA May 28 '16 at 12:48
  • Stop using getString and converting it to a JSONObject. Just use the getJsonObject method for objects and the gerJsonArray method for arrays – OneCricketeer May 28 '16 at 12:51
  • It was for me to trace as I didn't know how to chain the elements – JianYA May 28 '16 at 12:54
  • I understand that, but you can toString a JSONObject if you would like to log it – OneCricketeer May 28 '16 at 12:56
  • try this for testing: String url = jsonObject1.getJSONObject("responseData").getJSONObject("feed").getJSONArray("entries").getJSONOBject(0).getJSONArray("mediaGroups").getJSONObject(0).getJSONArray("contents")..getJSONObject(0).getString("url"); – Anoop Kanyan May 28 '16 at 13:02
  • That doesn't work either. It says no value for entries – JianYA May 28 '16 at 13:09
  • The thing is that if I set it as a JSONObject, a type mismatch comes out but the logs show that it has access the insides. – JianYA May 28 '16 at 13:14

2 Answers2

0

mediaGroup is a JsonArray instead of JsonObject. To access it do something like this

JSONObject jsonObj= jsonArray1.getJSONObject(i);
JSONArray media=jsonObj.getJSONArray("mediaGroups");

Your next problem is this

String entries = jsonObject3.getString("entries");

Entries is also a json Array so you have to get it also

JSONArray entries=jsonObject3.getJSONArray("entries");
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0

To access the contents array, do:

JSONArray contents = jsonArray1.getJSONObject(i).getJSONArray("mediaGroups").getJSONObject(0).getJSONArray("contents");

Then:

String url = contents.getJSONObject(i).getString("url");

To access the thumbnails array, do:

JSONArray thumbnails = contents.getJSONObject(i).getJSONArray("thumbnails");
String thumbNailURL = thumbnails.getJSONObject(0).getString("url");
Daniel
  • 2,355
  • 9
  • 23
  • 30
  • I can already get those items. I'm trying to get the url for the image inside the contents element – JianYA May 28 '16 at 12:59
  • I apologize, I misunderstood that. I'll update my answer. – Daniel May 28 '16 at 13:01
  • Sorry, the contents is supposed to be an int now – JianYA May 28 '16 at 13:16
  • Thanks, it works but gives a org.json.JSONException: Index 5 out of range [0..5) exception. This shows a list of URLs but when I remove the part after media groups, why does it lose its value? – JianYA May 28 '16 at 13:24
  • I want to be able to access the contents element first. I could access it before but suddenly i stopped being able to with my old methods. And what I mean is if I write this, JSONArray contents = jsonArray1.getJSONObject(i).getJSONArray("mediaGroups") , it wont work – JianYA May 28 '16 at 13:41
  • http://codebeautify.org/jsonviewer# I used this website and I could see the title, link, contentSnippet in the mediaGroups. what I want to go to is the contents where i can get the thumbnail urls. But i've run my code and i am still able to get the urls although with an exception that doesnt cause a crash. – JianYA May 28 '16 at 14:06
  • Look at the picture I posted. Every `entry` JSON object in the `entries` JSON array contains one mediagroup object & then all those fields. Yes you have an exception because contents is an array so you would need a for loop to access it to correctly but like I said, you are not being specific enough as to what you want. If you want the thumbnails themselves, that is another array. You just need to tell us exactly what you want. – Daniel May 28 '16 at 14:15
  • I actually wanted direct access to the thumbnails. – JianYA May 28 '16 at 14:25
  • Thanks, your contents.getJSONObject needs to be a string though. Thank you so much for your help – JianYA May 28 '16 at 14:44