首页 > 解决方案 > 无法提取 json 响应

问题描述

我正在尝试提取 JSON 响应。当我尝试访问 json 数组中的对象时,它返回 undefined

  weather=  [{"id":711,"main":"Smoke","description":"smoke","icon":"50d"}]
 var x=JSON.stringify(weather)
 x[0].main= returns =>undefined

标签: jsonreactjsaxios

解决方案


您可以简单地使用Array#map OR Array#forEach函数来获取所有 JSON 数据。您不需要JSON.stringify您的回复中使用。

演示:

let weather = [{
  "id": 711,
  "main": "Smoke",
  "description": "smoke",
  "icon": "50d"
}]

weather.map(function(x) {
  console.log(x.id) //711
  console.log(x.main) //Smoke
  console.log(x.description) //smoke
  console.log(x.icon) //50d
})


推荐阅读