Kira Kira - 1 month ago 12
JSON Question

Access elements in JSON Array in Java

Here i have results of json response, I need to get correct code to get access to the forecast array, How do i achieve this?

{
"query": {
"count": 1,
"created": "2016-10-23T02:37:21Z",
"lang": "en-US",
"results": {
"channel": {
"units": {
"distance": "mi",
"pressure": "in",
"speed": "mph",
"temperature": "F"
},
"title": "Yahoo! Weather - Kingston, Saint Andrew, JM",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-109251/",
"description": "Yahoo! Weather for Kingston, Saint Andrew, JM",
"language": "en-us",
"lastBuildDate": "Sat, 22 Oct 2016 09:37 PM EST",
"ttl": "60",
"location": {
"city": "Kingston",
"country": "Jamaica",
"region": " Saint Andrew"
},
"wind": {
"chill": "77",
"direction": "0",
"speed": "0"
},
"atmosphere": {
"humidity": "90",
"pressure": "988.0",
"rising": "0",
"visibility": "15.3"
},
"astronomy": {
"sunrise": "6:3 am",
"sunset": "5:40 pm"
},
"image": {
"title": "Yahoo! Weather",
"width": "142",
"height": "18",
"link": "http://weather.yahoo.com",
"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
},
"item": {
"title": "Conditions for Kingston, Saint Andrew, JM at 09:00 PM EST",
"lat": "18.015711",
"long": "-76.79731",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-109251/",
"pubDate": "Sat, 22 Oct 2016 09:00 PM EST",
"condition": {
"code": "27",
"date": "Sat, 22 Oct 2016 09:00 PM EST",
"temp": "77",
"text": "Mostly Cloudy"
},
"forecast": [
{
"code": "4",
"date": "22 Oct 2016",
"day": "Sat",
"high": "80",
"low": "75",
"text": "Thunderstorms"
},
{
"code": "4",
"date": "23 Oct 2016",
"day": "Sun",
"high": "80",
"low": "75",
"text": "Thunderstorms"
},
{
"code": "4",
"date": "24 Oct 2016",
"day": "Mon",
"high": "80",
"low": "76",
"text": "Thunderstorms"
},
{
"code": "4",
"date": "25 Oct 2016",
"day": "Tue",
"high": "80",
"low": "74",
"text": "Thunderstorms"
},
{
"code": "4",
"date": "26 Oct 2016",
"day": "Wed",
"high": "80",
"low": "75",
"text": "Thunderstorms"
},
{
"code": "4",
"date": "27 Oct 2016",
"day": "Thu",
"high": "80",
"low": "75",
"text": "Thunderstorms"
},
{
"code": "4",
"date": "28 Oct 2016",
"day": "Fri",
"high": "78",
"low": "74",
"text": "Thunderstorms"
}
],

"guid": {
"isPermaLink": "false"
}
}
}
}
}
}


This is what i was trying but it did not work. I just need to get correct access to the forecast JSONArray, whats the correct notation?

public void populate(JSONObject data) throws JSONException {

JSONArray jArr = data.getJSONObject("item").getJSONObject("condition").getJSONArray("forecast");


for(int i=0;i<jArr.length();i++){
JSONObject jDayForecast = jArr.getJSONObject(i);
String date = jDayForecast.getString("date");
String text = jDayForecast.getString("text");
}


}

This line always return null as it is not getting reference to forecast array?

JSONArray jArr = data.getJSONArray("forecast");

Answer

You get wrong object

JSONArray jArr = data.getJSONObject("query")
.getJSONObject("results")
.getJSONObject("channel")
.getJSONObject("item")
.getJSONArray("forecast");

If you are passing channel object to populate function :

JSONArray jArr = data.JSONObject("item")
.getJSONArray("forecast");
Comments