title: Referencing nested JSON Array
body: <p>I am attempting to create a weather app using Yahoo's api.</p>
  
  <p>Unfortunately, I don't know how to reference JSON, and although I have searched for solutions, they haven't been applicable to me.</p>
  
  <p>I am attempting to retrieve the array called forecast in :</p>
  
  <pre><code>{
      "query": {
          "count": 1,
          "created": "2016-06-22T16:12:10Z",
          "lang": "en-US",
          "results": {
              "channel": {
                  "units": {
                      "distance": "km",
                      "pressure": "mb",
                      "speed": "km/h",
                      "temperature": "C"
                  },
                  "title": "Yahoo! Weather - Sydney, NSW, AU",
                  "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-26198393/",
                  "description": "Yahoo! Weather for Sydney, NSW, AU",
                  "language": "en-us",
                  "lastBuildDate": "Thu, 23 Jun 2016 02:12 AM AEST",
                  "ttl": "60",
                  "location": {
                      "city": "Sydney",
                      "country": "Australia",
                      "region": " NSW"
                  },
                  "wind": {
                      "chill": "48",
                      "direction": "293",
                      "speed": "17.70"
                  },
                  "atmosphere": {
                      "humidity": "72",
                      "pressure": "34202.54",
                      "rising": "0",
                      "visibility": "25.91"
                  },
                  "astronomy": {
                      "sunrise": "7:1 am",
                      "sunset": "4:55 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 Sydney, NSW, AU at 01:00 AM AEST",
                      "lat": "-33.868999",
                      "long": "151.209045",
                      "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-26198393/",
                      "pubDate": "Thu, 23 Jun 2016 01:00 AM AEST",
                      "condition": {
                          "code": "31",
                          "date": "Thu, 23 Jun 2016 01:00 AM AEST",
                          "temp": "10",
                          "text": "Clear"
                      },
                      "forecast": [{
                          "code": "34",
                          "date": "23 Jun 2016",
                          "day": "Thu",
                          "high": "16",
                          "low": "10",
                          "text": "Mostly Sunny"
                      }, {
                          "code": "23",
                          "date": "24 Jun 2016",
                          "day": "Fri",
                          "high": "15",
                          "low": "10",
                          "text": "Breezy"
                      }, {
                          "code": "23",
                          "date": "25 Jun 2016",
                          "day": "Sat",
                          "high": "13",
                          "low": "8",
                          "text": "Breezy"
                      }, {
                          "code": "28",
                          "date": "26 Jun 2016",
                          "day": "Sun",
                          "high": "12",
                          "low": "6",
                          "text": "Mostly Cloudy"
                      }, {
                          "code": "11",
                          "date": "27 Jun 2016",
                          "day": "Mon",
                          "high": "10",
                          "low": "8",
                          "text": "Showers"
                      }, {
                          "code": "11",
                          "date": "28 Jun 2016",
                          "day": "Tue",
                          "high": "14",
                          "low": "10",
                          "text": "Showers"
                      }, {
                          "code": "30",
                          "date": "29 Jun 2016",
                          "day": "Wed",
                          "high": "15",
                          "low": "11",
                          "text": "Partly Cloudy"
                      }, {
                          "code": "34",
                          "date": "30 Jun 2016",
                          "day": "Thu",
                          "high": "16",
                          "low": "8",
                          "text": "Mostly Sunny"
                      }, {
                          "code": "34",
                          "date": "01 Jul 2016",
                          "day": "Fri",
                          "high": "16",
                          "low": "11",
                          "text": "Mostly Sunny"
                      }, {
                          "code": "23",
                          "date": "02 Jul 2016",
                          "day": "Sat",
                          "high": "12",
                          "low": "11",
                          "text": "Breezy"
                      }],
                      "description": "&lt;![CDATA[&lt;img src=\"http://l.yimg.com/a/i/us/we/52/31.gif\"/&gt;\n&lt;BR /&gt;\n&lt;b&gt;Current Conditions:&lt;/b&gt;\n&lt;BR /&gt;Clear\n&lt;BR /&gt;\n&lt;BR /&gt;\n&lt;b&gt;Forecast:&lt;/b&gt;\n&lt;BR /&gt; Thu - Mostly Sunny. High: 16Low: 10\n&lt;BR /&gt; Fri - Breezy. High: 15Low: 10\n&lt;BR /&gt; Sat - Breezy. High: 13Low: 8\n&lt;BR /&gt; Sun - Mostly Cloudy. High: 12Low: 6\n&lt;BR /&gt; Mon - Showers. High: 10Low: 8\n&lt;BR /&gt;\n&lt;BR /&gt;\n&lt;a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-26198393/\"&gt;Full Forecast at Yahoo! Weather&lt;/a&gt;\n&lt;BR /&gt;\n&lt;BR /&gt;\n(provided by &lt;a href=\"http://www.weather.com\" &gt;The Weather Channel&lt;/a&gt;)\n&lt;BR /&gt;\n]]&gt;",
                      "guid": {
                          "isPermaLink": "false"
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I've tried this solution:</p>
  
  <pre><code>final String OWM_QUERY = "query";
  final String OWM_RESULTS = "results";
  final String OWM_CHANNEL = "channel";
  final String OWM_ITEM = "item";
  final String OWM_FORECAST = "forecast";
  final String OWM_MAX = "high";
  final String OWM_MIN = "low";    
  
  JSONObject forecastJson = new JSONObject(forecastJsonStr);
  JSONArray weatherArray = forecastJson.getJSONObject(OWM_QUERY)
      .getJSONObject(OWM_RESULTS)
      .getJSONObject(OWM_CHANNEL)
      .getJSONObject(OWM_ITEM)
      .getJSONArray(OWM_FORECAST);
  </code></pre>
  
  <p>But this returns me a:</p>
  
  <blockquote>
    <p>Value null at results of type org.json.JSONObject$1 cannot be converted to JSONObject</p>
  </blockquote>
  
  <p>Error, so I assume the way I am trying to reference the array is incorrect.</p>
  
  <p>Thanks for any help in advance</p>
  
  <p>EDIT:</p>
  
  <p>Full Error:</p>
  
  <pre><code>06-23 03:02:27.570 18607-18710/com.example.alan.organiseme V/FetchWeatherTask: Forecast string: {"query":{"count":0,"created":"2016-06-22T17:02:08Z","lang":"en-US","results":null}}
  06-23 03:02:27.571 18607-18710/com.example.alan.organiseme E/FetchWeatherTask: Value null at results of type org.json.JSONObject$1 cannot be converted to JSONObject
  org.json.JSONException: Value null at results of type org.json.JSONObject$1 cannot be converted to JSONObject
     at org.json.JSON.typeMismatch(JSON.java:100)
     at org.json.JSONObject.getJSONObject(JSONObject.java:613)
     at com.example.alan.organiseme.weather$FetchWeatherTask.getWeatherDataFromJson(weather.java:130)
     at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:274)
     at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:78)
     at android.os.AsyncTask$2.call(AsyncTask.java:295)
     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
     at java.lang.Thread.run(Thread.java:818)
  06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err: org.json.JSONException: Value null at results of type org.json.JSONObject$1 cannot be converted to JSONObject
  06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err:     at org.json.JSON.typeMismatch(JSON.java:100)
  06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err:     at org.json.JSONObject.getJSONObject(JSONObject.java:613)
  06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err:     at com.example.alan.organiseme.weather$FetchWeatherTask.getWeatherDataFromJson(weather.java:130)
  06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err:     at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:274)
  06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err:     at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:78)
  06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
  06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
  06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
  06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
  06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err:     at java.lang.Thread.run(Thread.java:818)
  </code></pre>
  
  <p>Second Error</p>
  
  <pre><code>06-23 03:36:11.704 17086-17172/com.example.alan.organiseme V/FetchWeatherTask: Forecast string: {"query":{"count":1,"created":"2016-06-22T17:35:52Z","lang":"en-US","results":{"channel":{"units":{"distance":"km","pressure":"mb","speed":"km/h","temperature":"C"},"title":"Yahoo! Weather - Sydney, NSW, AU","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-55857444/","description":"Yahoo! Weather for Sydney, NSW, AU","language":"en-us","lastBuildDate":"Thu, 23 Jun 2016 03:35 AM AEST","ttl":"60","location":{"city":"Sydney","country":"Australia","region":" NSW"},"wind":{"chill":"45","direction":"338","speed":"11.27"},"atmosphere":{"humidity":"80","pressure":"33965.49","rising":"0","visibility":"25.91"},"astronomy":{"sunrise":"7:1 am","sunset":"4:55 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 Sydney, NSW, AU at 03:00 AM AEST","lat":"-33.783611","long":"151.145325","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-55857444/","pubDate":"Thu, 23 Jun 2016 03:00 AM AEST","condition":{"code":"31","date":"Thu, 23 Jun 2016 03:00 AM AEST","temp":"8","text":"Clear"},"forecast":[{"code":"30","date":"23 Jun 2016","day":"Thu","high":"15","low":"6","text":"Partly Cloudy"},{"code":"32","date":"24 Jun 2016","day":"Fri","high":"14","low":"7","text":"Sunny"},{"code":"32","date":"25 Jun 2016","day":"Sat","high":"12","low":"5","text":"Sunny"},{"code":"28","date":"26 Jun 2016","day":"Sun","high":"11","low":"3","text":"Mostly Cloudy"},{"code":"39","date":"27 Jun 2016","day":"Mon","high":"8","low":"6","text":"Scattered Showers"},{"code":"39","date":"28 Jun 2016","day":"Tue","high":"12","low":"6","text":"Scattered Showers"},{"code":"30","date":"29 Jun 2016","day":"Wed","high":"14","low":"7","text":"Partly Cloudy"},{"code":"34","date":"30 Jun 2016","day":"Thu","high":"15","low":"6","text":"Mostly Sunny"},{"code":"34","date":"01 Jul 2016","day":"Fri","high":"15","low":"8","text":"Mostly Sunny"},{"code":"34","date":"02 Jul 2016","day":"Sat","high":"12","low":"10","text":"Mostly Sunny"}],"description":"&lt;![CDATA[&lt;img src=\"http://l.yimg.com/a/i/us/we/52/31.gif\"/&gt;\n&lt;BR /&gt;\n&lt;b&gt;Current Conditions:&lt;/b&gt;\n&lt;BR /&gt;Clear\n&lt;BR /&gt;\n&lt;BR /&gt;\n&lt;b&gt;Forecast:&lt;/b&gt;\n&lt;BR /&gt; Thu - Partly Cloudy. High: 15Low: 6\n&lt;BR /&gt; Fri - Sunny. High: 14Low: 7\n&lt;BR /&gt; Sat - Sunny. High: 12Low: 5\n&lt;BR /&gt; Sun - Mostly Cloudy. High: 11Low: 3\n&lt;BR /&gt; Mon - Scattered Showers. High: 8Low: 6\n&lt;BR /&gt;\n&lt;BR /&gt;\n&lt;a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-55857444/\"&gt;Full Forecast at Yahoo! Weather&lt;/a&gt;\n&lt;BR /&gt;\n&lt;BR /&gt;\n(provided by &lt;a href=\"http://www.weather.com\" &gt;The Weather Channel&lt;/a&gt;)\n&lt;BR /&gt;\n]]&gt;","guid":{"isPermaLink":"false"}}}}}}
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme E/FetchWeatherTask: No value for forecast
                                                                                 org.json.JSONException: No value for forecast
                                                                                     at org.json.JSONObject.get(JSONObject.java:389)
                                                                                     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
                                                                                     at com.example.alan.organiseme.weather$FetchWeatherTask.getWeatherDataFromJson(weather.java:168)
                                                                                     at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:274)
                                                                                     at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:78)
                                                                                     at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                     at java.lang.Thread.run(Thread.java:818)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: org.json.JSONException: No value for forecast
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at com.example.alan.organiseme.weather$FetchWeatherTask.getWeatherDataFromJson(weather.java:168)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:274)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:78)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
  06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err:     at java.lang.Thread.run(Thread.java:818)
  </code></pre>