0

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

1
  • what are you passing to populate function ?! see my answer as i make it more clear Commented Oct 23, 2016 at 6:22

2 Answers 2

1

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

Comments

1

You could try this way :

try{
        /*Your response Object*/
        JSONObject responseObj = new JSONObject(mResponse);

        /*Getting query Object*/
        JSONObject queryObj = responseObj.getJSONObject("query");

        /*Getting result Object*/
        JSONObject resultObj = queryObj.getJSONObject("results");

        /*Getting channel Object*/
        JSONObject channelObj = resultObj.getJSONObject("channel");

        /*Getting item Object*/
        JSONObject itemObj = channelObj.getJSONObject("item");

        /*Getting forecast Array*/
        JSONArray forecastArray = itemObj.getJSONArray("forecast");

        /*Now you could iterate your array*/
        for(int i = 0 ; i < forecastArray.length(); i++){

            JSONObject forecastObj = forecastArray.getJSONObject(i);
            String code = forecastObj.getString("code");
            // As same as other parameters 
        }


    }catch (Exception e){

    }

3 Comments

@Kira : What type of error are you getting now? Check your input string. Are you passing right response string or not?
@Kira : Copy the full try catch block inside your populate function and pass basic response string as input parameter instead of JSONobject
Thanks guys, you both helped alot. I managed to fix the problem from understanding how hierachy of json elements work.

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.