user9427911
user9427911

Reputation:

Parse Yahoo's API response for weather forecast

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"
          }
        }
      }
    }
  }
}

Upvotes: 0

Views: 346

Answers (1)

Guido
Guido

Reputation: 47665

"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.

Upvotes: 1

Related Questions