首页 > 解决方案 > 如何访问嵌套在此 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
  }
}

标签: jsonreactjsobject

解决方案


使用 JSON.parse();

例子:

var data = JSON.parse(yourJSONObject)
console.log(data.city.name)

推荐阅读