jaroman777 jaroman777 - 3 months ago 13
JSON Question

Calling array withing an object. (Android, JSON)

Can I access the entries array without having to call the upper objects. Please provide me an example how can I access the entries array.
I am kind of new to this so it its very confusing when JSON data has different entries and methods of extractions. I guess I have not grasp the query pretty well. I do understand the difference between the array [] and the object {}, but I am still learning about the composition.
Thanks.


JSON:


{
"responseData":{
"feed":{
"feedUrl":"",
"title":"",
"link":"",
"author":"",
"description":"",
"type":"",
"entries":[
{
"title":"",
"link":"",
"author":"",
"publishedDate":"",
"contentSnippet":"",
"content":"",
"categories":[
""
]
},
{
"title":"",
"link":"",
"author":"",
"description":"",
"type":"",
"entries":[
{
"title":"",
"link":"",
"author":"",
"publishedDate":"",
"contentSnippet":"",
"content":"",
"categories":[
""
]
}
]
}
},
"responseDetails":null,
"responseStatus":200
}


Code:

HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("entries");
NewsList.content = (int)jarray.length();
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
NewsList News = new NewsList();
News.setId(object.getString("title"));
News.setName(object.getString("publishedDate"));
News.setAuthor(object.getString("link"));
NewsListList.add(News);
}

Answer

No. You can't get the entries array without having to call the upper objects.

You can parse above json response like that.

JSONObject jsono = new JSONObject(data);
JSONObject json1 = jsono.getJSONObject("responseData");
JSONObject json2 = json1.getJSONObject("feed");
String feedUrl = json2.getString("feedUrl");
JSONArray jarray = json2.getJSONArray("entries");
NewsList.content = (int)jarray.length();
for (int i = 0; i < jarray.length(); i++) {
 JSONObject object = jarray.getJSONObject(i);
 NewsList News = new NewsList();
 News.setId(object.getString("title"));
 News.setName(object.getString("publishedDate"));
 News.setAuthor(object.getString("link"));
 NewsListList.add(News);
}

Hope this will help you.