Jas*_*enX 2 javascript ecmascript-6
console.log(data); 返回:
duration: 60
location::
city: "New York"
state: "New York"
Run Code Online (Sandbox Code Playgroud)
但是console.log(data.location);回报Undefined。
那怎么可能?为什么不能访问嵌套对象?在这里,双冒号表示什么?
JSON中的属性“ location”实际上是“ location:”。
如果在编写对象属性时引用该对象属性(或更正数据或解析以确保其为有效JSON),则该对象属性应能按预期工作。
data["location:"]
Run Code Online (Sandbox Code Playgroud)
或者,通过更正数据:
{
"duration": 60,
"location": {
"city": "New York",
"state": "New York"
}
}
Run Code Online (Sandbox Code Playgroud)