首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析Yahoo的天气预报API响应

解析Yahoo的天气预报API响应
EN

Stack Overflow用户
提问于 2018-04-07 14:46:24
回答 1查看 320关注 0票数 0

我正在使用Yahoo API创建一个天气应用程序。我想要一个天气预报。

我试过使用JSONObject

代码语言:javascript
复制
 JSONObject jobj2 = new JSONObject(response);
 JSONArray jsonArray2 = jobj2.getJSONArray("forcast");
 for (int i = 0; i < jsonArray2.length(); i++) {
    ....
 }

但这显示了一个错误。

下面是我需要处理的API的响应:

代码语言:javascript
复制
{
  "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"
          }
        }
      }
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-07 14:59:25

"forecast“嵌套在JSON响应中的query > results > item > forecast下。对于getJSONArray("forcast"),您试图从根级别获取它,这是错误的。

您需要遍历树以获得嵌套的“预测”节点。看看解决了同样问题的How to access nested elements of json object using getJSONArray method

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49704715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档