从雅虎天气API获取一些东西

getting something from yahoo weather API

本文关键字:获取 API 雅虎      更新时间:2023-09-26

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

提前感谢!!

https://query.yahooapis.com/v1/public/yql?q=select%20*%20来自%20天气预报%20何处%20天气%20英寸%20(从%20地理位置(1)%20何地%20文本%3D%22绿地%22中选择%20天气;format=json&env=存储%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"
          }
        }
      }
    }
  }
}

您得到的响应是字符串,而不是对象。因此您必须使用JSON.parse(response)将其解析为对象。

我认为你可以尝试PostMan和JSONView,它将帮助你更容易地测试任何API。

希望这能帮助到你。

刚刚发现我必须做什么(讨厌js列表)。我用点记法来记temps。所以只是为了记录

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

如果有人能用[]表示法,那就太好了。谢谢你抽出时间。