0

I am creating a weather application using Yahoo API. I want to get a forecast.

I've tried using JSONObject:

 JSONObject jobj2 = new JSONObject(response);
 JSONArray jsonArray2 = jobj2.getJSONArray("forcast");
 for (int i = 0; i < jsonArray2.length(); i++) {
    ....
 }

But that shows an error.

This is the response from the API that I need to process:

{
  "query": {
    "count": 1,
    "created": "2018-04-07T05:03:39Z",
    "lang": "en-us",
    "results": {
      "channel": {
        "units": {
          "distance": "mi",
          "pressure": "in",
          "speed": "mph",
          "temperature": "F"
        },
        "title": "Yahoo! Weather - London, England, GB",
        "link": "...",
        "description": "Yahoo! Weather for London, England, GB",
        "language": "en-us",
        "lastBuildDate": "Sat, 07 Apr 2018 06:03 AM BST",
        "ttl": "60",
        "location": {
          "city": "London",
          "country": "United Kingdom",
          "region": " England"
        },
        "wind": {
          "chill": "48",
          "direction": "90",
          "speed": "7"
        },
        "atmosphere": {
          "humidity": "78",
          "pressure": "1002.0",
          "rising": "0",
          "visibility": "16.1"
        },
        "astronomy": {
          "sunrise": "6:21 am",
          "sunset": "7:45 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 London, England, GB at 05:00 AM BST",
          "lat": "51.506401",
          "long": "-0.12721",
          "link": "...",
          "pubDate": "Sat, 07 Apr 2018 05:00 AM BST",
          "condition": {
            "code": "26",
            "date": "Sat, 07 Apr 2018 05:00 AM BST",
            "temp": "49",
            "text": "Cloudy"
          },
          "forecast": [
            {
              "code": "26",
              "date": "07 Apr 2018",
              "day": "Sat",
              "high": "60",
              "low": "50",
              "text": "Cloudy"
            },
            {
              "code": "12",
              "date": "08 Apr 2018",
              "day": "Sun",
              "high": "53",
              "low": "48",
              "text": "Rain"
            }
          ],
          "description": "...",
          "guid": {
            "isPermaLink": "false"
          }
        }
      }
    }
  }
}
ekad
  • 14,436
  • 26
  • 44
  • 46
  • Can you add the error you get? – Guido Apr 07 '18 at 06:56
  • There's a [website](http://www.jsonschema2pojo.org/) where you can upload your JSON and get the corresponding POJO schema.. Then you can use libraries like Gson to convert the Yahoo's response to that Pojo- with a few lines of code. Moreover if you're using library like Retrofit, then it will be a piece of cake. – realpac Apr 07 '18 at 07:27

1 Answers1

1

"forecast" is nested under query > results > item > forecast in the JSON response. With getJSONArray("forcast") you're trying to get it from the root level, which is wrong.

You need to traverse the tree to get the nested "forecast" node. Take a look at How to access nested elements of json object using getJSONArray method that solves the same issue.

Guido
  • 46,642
  • 28
  • 120
  • 174
  • @angelina to help other people, mark the answer as accepted if it solved your issue or just close the question if it is a duplicate. – Guido Apr 13 '18 at 19:02