简体   繁体   English

如何使用 JavaScript 访问 JSON 文档的属性?

[英]How do I access properties of a JSON document using JavaScript?

Using JavaScript , how can I access the area variable in the JSON document below?使用JavaScript ,如何访问下面JSON文档中的area变量?

{

  "foo : age=39, height=170": {
  "age": 39,
  "height": 172,
  "center": {
     "area": 102,
     "local": 304
  },
  "color": "#0000ff",
  "radius": 9.5,
  "color_srv": "#aa0054"
 }
}

You need the whole property string for the access, even if it look a bit strange.您需要访问整个属性字符串,即使它看起来有点奇怪。

 var data = { "foo : age=39, height=170": { "age": 39, "height": 172, "center": { "area": 102, "local": 304 }, "color": "#0000ff", "radius": 9.5, "color_srv": "#aa0054" } }; document.write(data['foo : age=39, height=170'].center.area);

First you need to fix up that JSON , the format is invalid as it stands...首先,您需要修复该JSON ,该格式目前无效...

If you were to change it to something like this...如果你把它改成这样......

{
    "foo": {
        "age": 39,
        "height": 172,
        "center": {
            "area": 102,
            "local": 304
        },
        "color": "#0000ff",
        "radius": 9.5,
        "color_srv": "#aa0054"
    }
}

You could access that variable using the JavaScript below...您可以使用下面的JavaScript访问该变量...

console.log(testing["foo"]["center"]["area"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM