Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从json数据中获取键和值_Jquery_Json - Fatal编程技术网

Jquery 从json数据中获取键和值

Jquery 从json数据中获取键和值,jquery,json,Jquery,Json,我得到以下数据 alert(JSON.stringify(result.results[0])); 但获取的值为未定义。 这是更新的部分 result.results[0].test $.get('/getStatus',getdata,函数(data){ data.forEach(函数(testreport){ var report=JSON.stringify(testreport); 警报(报告); }); }); 这里有警报指纹 { “_id”:“5319b5e10748a607

我得到以下数据

alert(JSON.stringify(result.results[0]));
但获取的值为
未定义。

这是更新的部分

result.results[0].test 
$.get('/getStatus',getdata,函数(data){
data.forEach(函数(testreport){
var report=JSON.stringify(testreport);
警报(报告);
}); 
});
这里有警报指纹
{
“_id”:“5319b5e10748a6078fe4f360”,
“acces”:“172.1.6.2.18”,
“适配器”:“赢10”,
“味道”:“VM-IE8-001-preq1”,
“id”:“67”,
“操作系统”:“VM-WIN7-64”,
“结果”:[{
“\“测试\:\“连接\断开连接\”,“操作系统\:“Windows NT\”,“报告\:\”验证无线接口是否存在且状态是否已断开:确定
验证客户端上不存在配置文件:确定
将配置文件添加到客户端:确定
验证客户端上是否存在添加的配置文件:确定
连接到接入点:确定
验证状态是否已连接:确定
从接入点断开:确定
验证状态是否已断开:确定
删除Pr客户端的配置文件:确定
验证客户端上不存在配置文件:确定
\“”: }], “测试”:“Test01” }
它是一个对象,而不是数组。没有0索引,只有散列或属性。使用以下命令:


result.test

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

检查

var结果={
“\“测试\:\“连接\断开连接\”,“操作系统\:“Windows NT\”,“报告\:\”验证无线接口是否存在且状态是否已断开:确定
验证客户端上不存在配置文件:确定
将配置文件添加到客户端:确定
验证客户端上是否存在添加的配置文件:确定
连接到接入点:确定
验证状态是否已连接:确定
从接入点断开:确定
验证状态是否已断开:确定
删除Pr客户端的配置文件:确定
验证客户端上不存在配置文件:确定
\“”: }; var resultsValid={ “测试”:“连接\断开连接”, “操作系统”:“Windows NT” }; 警报(results.test) 警报(resultsValid.test) 警报(resultsValid.os)
检查您是如何生成此响应的


另外,您可以在

上验证json数据。您可以尝试使用jquery的
getJSON()
方法,并通过以下方式实现它:

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)

当然,是的,@Mate是正确的。

是的,我也尝试了你的代码。但未定义的值更新了我的问题。请查看this@Sush尝试JSON.parse而不是JSON.stringify
result。results
有一个元素,一个对象有一个属性,属性名为长字符串:
“\“test\”:\“Connect\u Disconnect\”,\”操作系统\“:\“Windows NT\”,\“报告\:\”验证无线接口是否存在且状态是否已断开:确定
验证客户端上不存在配置文件:确定
将配置文件添加到客户端:确定
验证客户端上是否存在添加的配置文件:确定
连接到接入点:确定
验证状态是否已连接:确定
从接入点断开:确定
验证状态是否已断开:确定
删除客户端配置文件:确定
验证客户端上不存在配置文件:确定
\“”
…其中属性名称包含引号和冒号,看起来像JSON本身。该属性的值(如果向右滚动)是空字符串。你可以在这里看到:。换句话说,您的结构在技术上是有效的,但不是您所需要的返回给你?
result.results[0].test 
     $.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"
    }
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)
$.getJSON( "enterYourUrlHere", function( result ) {
    alert(result.results[0].test)
});