从json数据中获取键和值

时间:2014-03-08 05:26:35

标签: jquery json

  result=  {
        "_id": "5319b5e10748a6078fe4f360",
        "acces": "172.1.6.2.18",        
        "results": [{
            "\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
        }],
        "tests": "Test01"
    }

当我试图提醒

alert(JSON.stringify(result.results[0]));

我正在获取以下数据

{
    "\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
}

从中我如何获得test,os and report

的值

我试过

result.results[0].test 

但价值为undefined.

这是更新的部分

     $.get('/getStatus', getdata, function (data) {
           data.forEach(function (testreport) {
var report = JSON.stringify(testreport);
alert(report);

        }); 


      });
here alert prints



 {
        "_id": "5319b5e10748a6078fe4f360",
        "acces": "172.1.6.2.18",
        "adapter": "Win 10",
        "flavour": "VM-IE8-001-preq1",
        "id": "67",
        "os": "VM-WIN7-64",
        "results": [{
            "\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
        }],
        "tests": "Test01"
    }

3 个答案:

答案 0 :(得分:1)

你的结果。结果是一个字符串,而不是一个json。

检查DEMO

var results = {
    "\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
};


var resultsValid = {
    "test": "\"Connect_Disconnect\"",
    "os": "\"Windows NT\""
};


alert(results.test)

alert(resultsValid.test)
alert(resultsValid.os)

检查您是如何生成此响应的

此外,您可以在http://jsonlint.com/

上验证json数据

答案 1 :(得分:0)

它是一个对象,而不是一个数组。没有0索引,只是一个哈希或属性。用这个:

result.test

答案 2 :(得分:0)

你能尝试使用jquery的getJSON()方法,并以这种方式实现它:

$.getJSON( "enterYourUrlHere", function( result ) {
    alert(result.results[0].test)
});

而且,是的,@ May是正确的。