JianYA JianYA - 6 months ago 16
Android Question

Android JSONException no value for mediaGroup

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

Answer

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