我正在尝试处理来自多个城市的OpenWeather API的数据。数据如下所示:
{
"cnt":20,
"list":[
{
"coord":{"lon":-99.13,"lat":19.43},
"sys":{"country":"MX","timezone":-18000,"sunrise":1591012665,"sunset":1591060290},
"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],
"main":{"temp":12.88,"feels_like":11.84,"temp_min":12.78,"temp_max":13,"pressure":1027,"humidity":82},
"visibility":8047,
"wind":{"speed":1.5,"deg":60},
"clouds":{"all":20},
"dt":1591013691,
"id":3530597,
"name":"Mexico City"},
{...next cities...}
]
}
除了天气部分(第3行)之外,我一切都很好。我想将每个值分配给一个单独的变量,如下所示:
weatherid = 801
main = "Clouds"
description = "few clouds"
我已经尝试过类似的东西
main = citydata.list[i].weather.main; (doesn't work)
main = citydata.list[i].weather.main(); (doesn't work)
main = Object.values(citydata.list[i].weather);
及其之间的很多事情。
(嗯,最后一种有点,但是我并没有得到不同的结果,当我尝试获取值时,它总是“未定义”)
因此,访问该数据并将其各部分存储在其自己的变量中的最简单方法是什么。 甚至只是将“ main”的内容转换为数组(我假设这些问题重叠)
谢谢!
答案 0 :(得分:0)
尝试:
const data = JSON.parse(req.data);
const main = data.list[0].weather.main