从雅虎天气API获取一些东西
getting something from yahoo weather API
我正在尝试创建一个简单的应用程序来获取城市名称,并使用雅虎天气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
如果有人能用[]表示法,那就太好了。谢谢你抽出时间。
相关文章:
- JavaScript嵌套获取API
- 获取 API 响应对象并替换(映射)所有键
- 是否可以覆盖本机获取 API 以使用所需的承诺库而不是本机浏览器承诺
- 成功获取 API,但没有响应
- Javascript获取api:Can't检索某些响应标头
- javascript中获取api中的Url查询字符串
- 如果我想要的URL在API中,如何获取API的URL's的JSON
- 在jQuery中简单地获取API调用
- 获取API时带有html5mode的ExpressJS无法工作
- angular试图从一个不存在的源中获取api检查
- 如何在获取 API 中处理 HTTP 代码 4xx 响应
- 获取API回调的参数
- 使用请求获取API结果,并集成到Hubot响应中
- 获取API -跨域
- 关于从DS获取api数据.RESTAdapter到模型中
- 获取api - get json主体在then和catch块为单独的状态码
- fotorama:can't获取API对象(未定义)
- 获取API返回状态代码0,需要XML响应
- 使用javascript获取api来执行POST请求
- 可以't获取API响应的特定元素[JS]