使用jquery从json数据中获取值

时间:2014-03-08 04:53:26

标签: jquery json

我有像这样的json数据

{
    "_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>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"
}

从此 我必须得到值

adapter : "Win 10"
test : Connect_Disconnect
os :Windows NT

report : Verify Wireless Interface present and state is Disconnected:OK
Verify Profile not present on the Client:OK
Verify the State is Disconnected:OK
Delete Profile to the Client:OK
verify Profile Not Present on the Client:OK

我如何获得上述值

我试过

 data.forEach(function (testreport) {

alert(testreport.results.test);// getting value as undefined

}

2 个答案:

答案 0 :(得分:0)

这可以帮到你

 json_object._id;
 json_object.acces
 json_object.adapter
 json_object.flavour
 json_object.id
 json_object.os
 json_object.results
 json_object.results[0].os
 json_object.tests

答案 1 :(得分:0)

大多数浏览器都支持JSON.parse()
     你可以使用javascript来解析json或jquery

   var json = '{"result":true,"count":1}',
   obj = JSON.parse(json);
   alert(obj.count);

对于不支持json.parse()的浏览器,您可以使用

  var json = '{"result":true,"count":1}',
obj = JSON && JSON.parse(json) || $.parseJSON(json);