-3

I am new to JSON. I know the forecast is an array, but how do I access elements date and text? I know where the problem is: It seems i cannot get access to forecast array, as this gives a null.

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

what is the proper notation to get access to the forecast array. I was trying

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

but this notation still did not give me access to the array. What notation will give me correct reference to forecast array?

 "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": "32",
   "date": "20 Oct 2016",
   "day": "Thu",
   "high": "35",
   "low": "26",
   "text": "Sunny"
  },
  {
   "code": "34",
   "date": "21 Oct 2016",
   "day": "Fri",
   "high": "34",
   "low": "28",
   "text": "Mostly Sunny"
  },
  {
   "code": "26",
   "date": "22 Oct 2016",
   "day": "Sat",
   "high": "37",
   "low": "32",
   "text": "Cloudy"
  },
  {
   "code": "23",
   "date": "23 Oct 2016",
   "day": "Sun",
   "high": "37",
   "low": "34",
   "text": "Breezy"
  },

I was trying something like this, but my android app was crashing? I just need help in accessing the elements in the array to set them on some textfields.

public void populate(JSONObject data) throws JSONException {

    JSONArray jArr = data.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");
    }
}

here is the error in stacktrace.

54.516 27220-27220/net.digitalphantom.app.weatherapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: net.digitalphantom.app.weatherapp, PID: 27220
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String net.JamCast.app.weatherapp.data.Forecast.getDate()' on a null object reference
                                                                                   at net.JamCast.app.weatherapp.WeatherActivity$override.serviceSuccess(WeatherActivity.java:363)
                                                                                   at net.JamCast.app.weatherapp.WeatherActivity$override.access$dispatch(WeatherActivity.java)
                                                                                   at net.JamCast.app.weatherapp.WeatherActivity.serviceSuccess(WeatherActivity.java:0)
                                                                                   at net.JamCast.app.weatherapp.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:95)
                                                                                   at net.JamCast.app.weatherapp.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:37)
                                                                                   at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                                   at android.os.AsyncTask.access$500(AsyncTask.java:180)
                                                                                   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:158)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
JohnnyCage
  • 37
  • 7

1 Answers1

0

define a java bean class, use GSON jar translate json string to class object, then you can get text element by your object. please search in google sample.

yan
  • 41
  • 4