【发布时间】:2020-10-07 23:02:59
【问题描述】:
我正在尝试从 OpenWeatherMap 的 API 访问一些嵌套数据,这是我正在使用的示例:
{
"coord":{
"lon":-74.46,
"lat":40.55
},
"weather":[
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01d"
}
],
"base":"stations",
"main":{
"temp":67.93,
"feels_like":68.72,
"temp_min":64.99,
"temp_max":70,
"pressure":1022,
"humidity":77
},
"visibility":16093,
"wind":{
"speed":4.7,
"deg":100
},
"clouds":{
"all":1
},
"dt":1592439281,
"sys":{
"type":1,
"id":5874,
"country":"US",
"sunrise":1592386010,
"sunset":1592440257
},
"timezone":-14400,
"id":0,
"name":"Piscataway",
"cod":200
}
现在在我从 API 获取数据的函数内部,我正在尝试执行以下操作:
var weatherURL = 'http://api.openweathermap.org/data/2.5/weather?zip=08854,us&appid=(api key hidden for privacy)&units=imperial';
async function getWeather() {
const response = await fetch(weatherURL);
const data = await response.json();
console.log(data);
const {name, temp} = data;
document.getElementById('currentCity').textContent = name;
document.getElementById('temp').textContent = data["main"]["temp"];
}
.textContent = name 的工作方式与我的预期一样,但我将如何更正函数的最后一行以便可以访问 main.temp?
【问题讨论】:
标签: javascript html json openweathermap