我正在使用Openweathe map的每小时预报API调用来构建一个天气应用程序。我可以正确地获取调用的URL,但显示的JSON结构似乎与示例中提供的结构不匹配。我无法访问data.list
、data.sys
或data.city.name
。而我可以访问data.name
。
以下是JSON示例
{
cod: "200",
message: 0.0208,
cnt: 96,
list: [
{
dt: 1553709600,
main: {
temp: 286.44,
temp_min: 286.258,
temp_max: 286.44,
pressure: 1015.82,
sea_level: 1015.82,
grnd_level: 1002.193,
humidity: 100,
temp_kf: 0.18
},
weather: [
{
id: 500,
main: "Rain",
description: "light rain",
icon: "10d"
}
],
clouds: {
all: 86
},
wind: {
speed: 5.51,
deg: 202.816
},
rain: {
1h: 0.812
},
sys: {
pod: "d"
},
dt_txt: "2019-03-27 18:00:00"
},
...
],
city: {
id: 420006353,
name: "Mountain View",
coord: {
lat: 37.3894,
lon: -122.0833
},
country: "US"
}
}
下面是我用来管理调用的代码
function call(city,country,zip) {
let countryCode = list.getCode(country);
let API;
if (city!="" && country!="") {
API = 'http://api.openweathermap.org/data/2.5/weather?q='+city.toLowerCase()+','+countryCode.toLowerCase()+'&APPID=1a9b84b61d1a8d6fdbb52fa2800ef894';
}
else if(country!="" && zip!=""){
API ='http://api.openweathermap.org/data/2.5/forecast/hourly?zip='+zip.toString()+','+countryCode.toLowerCase()+'&APPID=1a9b84b61d1a8d6fdbb52fa2800ef894';
}
else{
alert("Please type your city or Zip code");
}
return API;
}
function getPlace() {
let city = cityInput.value;
let country = countryInput.value;
let zip = zipInput.value;
let APICall=call(city,country,zip);
fetch(APICall).then(response=>{
return response.json();
}).then(data=>{
let cityName= data.name;
settler(cityName);
})
}
我应该能够访问data.list
,但是alert(data.list.lenght)
给了我一个错误,说不能定义未定义的长度。对于不能访问未定义的name属性的alert(data.city.name)
也是如此。
发布于 2019-09-30 02:19:54
weather
应用程序接口文档正确地显示了weather
查询所需的数据:
https://openweathermap.org/current
如果命中了(暴露的;您应该修复的)API键对其无效的正确端点(pro
),那么您的hourly
查询应该可以工作。
pricing page表示hourly
数据不可免费使用。
https://stackoverflow.com/questions/58157886
复制相似问题