我有这个example我试图访问 json 值,但它甚至不产生任何警报。有什么问题吗?
我的 JSON
{
"response": [
{
"id": "0",
"elementName": "osname",
"isEqual": true,
"isPrasentinXml1": true,
"isPrasentinXml2": true,
"attribute": [
{
"name": "osname",
"firstValue": "Linux\u000a",
"secondValue": "SunOs\u000a"
}
]
},
{
"id": "1",
"elementName": "hostname",
"isEqual": false,
"isPrasentinXml1": true,
"isPrasentinXml2": true,
"attribute": [
{
"name": "hostname",
"firstValue": "estilo\u000a",
"secondValue": "buckeye.informatica.com\u000a"
}
]
}
]
}
我想获取Linux\u000a
和SunOs\u000a
,所以我写了
alert(compareData.response[0].attribute[0].firstValue+", "+compareData.response[0].attribute[0].secondValue);
注意:compareData
是我的数据在实际代码中的位置
最佳答案
您的错误是您的 JSON 周围有引号,并且它被视为字符串。此外,您还忘记在第二个 alert
中将 compareData
变量名称替换为 jsonobj
。尝试下面的 fiddle ,看来这就是你想要的。
jsFiddle:http://jsfiddle.net/Dna9H/6/
编辑:
如果您的 JSON 确实由字符串表示,请查看 Michael Sagalovich 解决方案。
关于javascript - 访问 JSON 字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6705311/