0

我正在尝试创建一个简单的应用程序来获取城市名称并使用雅虎天气 API 显示天气。我能够发出一个 json 请求并得到答案,但我完全不知道如何从这个 json 中检索信息。我可以看到 response.query ,但是当我尝试 response.query.something 时,我得到了未定义。有人可以向我解释如何获得 response.query.results.city 的东西吗?

提前致谢!!

https://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)% 20where%20text%3D%22greenland%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

{
  "query": {
    "count": 1,
    "created": "2015-11-23T16:18:30Z",
    "lang": "vi",
    "results": {
      "channel": {
        "title": "Yahoo! Weather - Greenland, GL",
        "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html",
        "description": "Yahoo! Weather for Greenland, GL",
        "language": "en-us",
        "lastBuildDate": "Mon, 23 Nov 2015 12:50 pm CGT",
        "ttl": "60",
        "location": {
          "city": "Greenland",
          "country": "Greenland",
          "region": ""
        },
        "units": {
          "distance": "mi",
          "pressure": "in",
          "speed": "mph",
          "temperature": "F"
        },
        "wind": {
          "chill": "15",
          "direction": "220",
          "speed": "15"
        },
        "atmosphere": {
          "humidity": "63",
          "pressure": "29.8",
          "rising": "0",
          "visibility": "6.21"
        },
        "astronomy": {
          "sunrise": "10:45 am",
          "sunset": "1:33 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 Greenland, GL at 12:50 pm CGT",
          "lat": "71.8",
          "long": "-42.18",
          "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html",
          "pubDate": "Mon, 23 Nov 2015 12:50 pm CGT",
          "condition": {
            "code": "28",
            "date": "Mon, 23 Nov 2015 12:50 pm CGT",
            "temp": "27",
            "text": "Mostly Cloudy"
          },
          "description": "\n<img src=\"http://l.yimg.com/a/i/us/we/52/28.gif\"/><br />\n<b>Current Conditions:</b><br />\nMostly Cloudy, 27 F<BR />\n<BR /><b>Forecast:</b><BR />\nMon - AM Clouds/PM Sun. High: 29 Low: 17<br />\nTue - Mostly Cloudy. High: 19 Low: 8<br />\nWed - PM Snow Showers. High: 12 Low: 6<br />\nThu - Mostly Cloudy. High: 10 Low: 0<br />\nFri - Mostly Sunny. High: 1 Low: -8<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n",
          "forecast": [
            {
              "code": "30",
              "date": "23 Nov 2015",
              "day": "Mon",
              "high": "29",
              "low": "17",
              "text": "AM Clouds/PM Sun"
            },
            {
              "code": "28",
              "date": "24 Nov 2015",
              "day": "Tue",
              "high": "19",
              "low": "8",
              "text": "Mostly Cloudy"
            },
            {
              "code": "14",
              "date": "25 Nov 2015",
              "day": "Wed",
              "high": "12",
              "low": "6",
              "text": "PM Snow Showers"
            },
            {
              "code": "28",
              "date": "26 Nov 2015",
              "day": "Thu",
              "high": "10",
              "low": "0",
              "text": "Mostly Cloudy"
            },
            {
              "code": "34",
              "date": "27 Nov 2015",
              "day": "Fri",
              "high": "1",
              "low": "-8",
              "text": "Mostly Sunny"
            }
          ],
          "guid": {
            "isPermaLink": "false",
            "content": "GLXX0012_2015_11_27_7_00_CGT"
          }
        }
      }
    }
  }
}
4

2 回答 2

0

您得到的响应是String,而不是Object,因此您必须使用将其解析为 Object JSON.parse(response)

我认为您可以尝试PostManJSONView,它将帮助您更轻松地测试任何 API。

希望这可以帮到你。

于 2015-11-23T16:29:42.957 回答
0

刚刚发现我必须做什么(讨厌的 js 列表)。我使用点符号来获取我的临时工。所以只是为了记录

var response; // here i have the jsonparse thing
//I want to access something I have to do 
response.query.results.channel.wind.speed //and i will get wind's speed

如果有人可以使用 [] 符号,那就太好了。谢谢你的时间 。

于 2015-11-23T17:54:03.857 回答