Javascript 访问JSON字符串值

Javascript 访问JSON字符串值,javascript,jquery,json,Javascript,Jquery,Json,我在这里尝试访问json值,但它甚至不会生成任何警报。有什么问题 我的JSON { "response": [ { "id": "0", "elementName": "osname", "isEqual": true, "isPrasentinXml1": true, "isPrasentinXml2": true, "attribute": [ { "name": "osname", "firstValue": "Linux\u000

我在这里尝试访问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周围有引号,它被视为字符串。另外,在第二个
警报中,您忘记将
compareData
变量名替换为
jsonobj
。试试下面的小提琴,它似乎就是你想要的

jsFiddle:

编辑:


如果您的JSON真的是由字符串表示的,请查看Michael Sagalovich解决方案。

您的错误是,您的JSON周围有引号,并且它被视为字符串。另外,在第二个
警报中,您忘记将
compareData
变量名替换为
jsonobj
。试试下面的小提琴,它似乎就是你想要的

jsFiddle:

编辑:

如果您的JSON确实由字符串表示,请查看Michael Sagalovich解决方案。

试试这个

试试这个


据我在小提琴上看到的,你的对象就是弦。您需要首先将其解析为一个对象
compareData=JSON.parse(compareData)
可能会有所帮助(我认为大多数浏览器都支持
JSON.parse
)。jQuery还有解析器:
$.parseJSON(compareData)

据我在小提琴中看到的,您的对象就是字符串。您需要首先将其解析为一个对象
compareData=JSON.parse(compareData)
可能会有所帮助(我认为大多数浏览器都支持
JSON.parse
)。jQuery还有解析器:
$.parseJSON(compareData)
您的语法。这项工作:

var jsonobj = {
    "response": [
        {
        "id": "0",
        "elementName": "osname",
        "isEqual": true,
        "isPrasentinXml1": true,
        "isPrasentinXml2": true,
        "attribute": [{
            "name": "osname",
            "firstValue": "Linux\u000a",
            "secondValue": "Linux\u000a"}]
        },
        {
        "id": "1",
        "elementName": "hostname",
        "isEqual": false,
        "isPrasentinXml1": true,
        "isPrasentinXml2": true,
        "attribute": [{
            "name": "hostname",
            "firstValue": "estilo\u000a",
            "secondValue": "buckeye.informatica.com\u000a"}]
        }
    ]
 };

alert(jsonobj.response[0].elementName);
alert(jsonobj.response[0].attribute[0].firstValue + ", " + jsonobj.response[0].attribute[0].secondValue);

请允许我问一下,您是否可以安装并学习如何使用Firebug或Chrome的开发控制台。这些工具可以让你的生活变得更加轻松。

你的语法。这项工作:

var jsonobj = {
    "response": [
        {
        "id": "0",
        "elementName": "osname",
        "isEqual": true,
        "isPrasentinXml1": true,
        "isPrasentinXml2": true,
        "attribute": [{
            "name": "osname",
            "firstValue": "Linux\u000a",
            "secondValue": "Linux\u000a"}]
        },
        {
        "id": "1",
        "elementName": "hostname",
        "isEqual": false,
        "isPrasentinXml1": true,
        "isPrasentinXml2": true,
        "attribute": [{
            "name": "hostname",
            "firstValue": "estilo\u000a",
            "secondValue": "buckeye.informatica.com\u000a"}]
        }
    ]
 };

alert(jsonobj.response[0].elementName);
alert(jsonobj.response[0].attribute[0].firstValue + ", " + jsonobj.response[0].attribute[0].secondValue);
请允许我问一下,您是否可以安装并学习如何使用Firebug或Chrome的开发控制台。这些工具可以让你的生活变得更轻松