javascript - 访问 JSON 字符串值

标签 javascript jquery json

我有这个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\u000aSunOs\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/

相关文章:

javascript - Ajax 表单不显示 html

javascript - 每次点击后的确认模式。第一次有效,然后失败。 (模态上的模态)

javascript - vuejs - 如何使用点击事件附加 html

javascript - 推送器未正确发布到服务器

javascript - 调用 API 端点时的 setTimeout 不返回值

javascript - 学习Javascript : comma after member array declaration?

javascript - Node.js 和 C# 之间的 Math.sin() 精度不同

javascript - 数据目标在 bootgrid 插件内不起作用

jquery - __RequestVerificationToken 未使用 axios 0.17.1 和 MVC 3/4/5 正确发送

javascript - 移动 json 数组中的项目