与JS关键字“ all”同名的JSON obj属性

时间:2018-10-29 16:04:19

标签: javascript

我正在尝试访问通过ajax调用返回的属性,并且正在返回的JSON对象具有我需要访问的属性,该属性的名称与关键字“ all”相同。我似乎找不到能够访问此属性字段的方法,因为它假定我在尝试访问属性“ forecast.main.clouds.all”时正在使用关键字,这是该对象的示例通过ajax调用返回。

{
"coord":{"lon":-93.3,"lat":37.26}, 
"weather":
 [ {
     "id":800,
      "main":"Clear",
      "description":"clear sky",
      "icon":"01d"
   } ], 
 "base":"stations", 
 "main": 
  {
    "temp":64.4, 
    "pressure":1019, 
    "humidity":36, 
    "temp_min":64.4, 
    "temp_max":64.4 
   }, 
  "visibility":16093, 
  "wind":{"speed":9.17,"deg":330}, 
  "clouds":{"all":1}, 
  "dt":1540759920, 
  "sys":{ 
        "type":1,
        "id":1661,
        "message":0.0041,
        "country":"US",
        "sunrise":1540730075,
        "sunset":1540768719 
      }, 
  "id":420021920, 
  "name":"Springfield", 
  "cod":200 

}

2 个答案:

答案 0 :(得分:0)

使用方括号forcast['clouds']['all']访问。

答案 1 :(得分:0)

它正常工作 all在JavaScript中不是keyword,并且根据您显示的代码段,似乎没有出现问题:

var forecast={
"coord":{"lon":-93.3,"lat":37.26}, 
"weather":
 [ {
     "id":800,
      "main":"Clear",
      "description":"clear sky",
      "icon":"01d"
   } ], 
 "base":"stations", 
 "main": 
  {
    "temp":64.4, 
    "pressure":1019, 
    "humidity":36, 
    "temp_min":64.4, 
    "temp_max":64.4 
   }, 
  "visibility":16093, 
  "wind":{"speed":9.17,"deg":330}, 
  "clouds":{"all":1}, 
  "dt":1540759920, 
  "sys":{ 
        "type":1,
        "id":1661,
        "message":0.0041,
        "country":"US",
        "sunrise":1540730075,
        "sunset":1540768719 
      }, 
  "id":420021920, 
  "name":"Springfield", 
  "cod":200 
};
console.log(forecast.clouds.all);

有什么问题吗?