json - 如何访问嵌套在此 api 响应中的城市名称?
问题描述
我使用 axios.get(...) 请求数据,成功获取作为嵌套对象的返回。我可以访问 data.message、data.cnt、data.cod 但我无法访问“城市”的属性。不断得到未定义或类型错误。
我想访问嵌套在响应数据中的城市对象的属性。像这个“data.city.name”但是错误。
{
"cod": "200",
"message": 0.0051,
"cnt": 40,
"list": [
{
"dt": 1545318000,
"main": {
"temp": 282.74,
"temp_min": 282.167,
"temp_max": 282.74,
"pressure": 1012.86,
"sea_level": 1020.54,
"grnd_level": 1012.86,
"humidity": 84,
"temp_kf": 0.57
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"clouds": { "all": 44 },
"wind": { "speed": 5.67, "deg": 243.503 },
"rain": { "3h": 0.25 },
"sys": { "pod": "d" },
"dt_txt": "2018-12-20 15:00:00"
},
{
"dt": 1545739200,
"main": {
"temp": 282.628,
"temp_min": 282.628,
"temp_max": 282.628,
"pressure": 1037.58,
"sea_level": 1045.29,
"grnd_level": 1037.58,
"humidity": 100,
"temp_kf": 0
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"clouds": { "all": 76 },
"wind": { "speed": 2.02, "deg": 212.503 },
"rain": { "3h": 0.13 },
"sys": { "pod": "d" },
"dt_txt": "2018-12-25 12:00:00"
}
],
"city": {
"id": 2643743,
"name": "London",
"coord": { "lat": 51.5073, "lon": -0.1277 },
"country": "GB",
"population": 1000000
}
}
解决方案
使用 JSON.parse();
例子:
var data = JSON.parse(yourJSONObject)
console.log(data.city.name)
推荐阅读
- python - pytesseract 无法识别字母前的符号
- c# - BigQuery BigQueryResults 结果到 json
- javascript - 移动 Safari 和 Chrome - ontimeupdate 不起作用,在桌面上工作
- postgresql - 使用 pd_dumpall 而不是 pg_dump 备份 postgres 时的自定义格式 (-Fc)
- spring-boot - 千分尺弹簧遗产在带有 ClassNotFound 问题的 springboot 1.4.7 中不起作用
- php - Laravel 直到页面加载后才看到用户已登录
- folium - 如何将folium地图库的地图类型更改为Satelite或Terrain等?
- python - 如何使用一个 POST 路由以不同的方法接受数组或对象?
- prestashop - Prestashop 1.7:当订单免费时使用哪个钩子(不付款)
- php - 需要拆分数字值php的帮助