Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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
用javascript解析JSON_Javascript_Jquery_Json_Parsing - Fatal编程技术网

用javascript解析JSON

用javascript解析JSON,javascript,jquery,json,parsing,Javascript,Jquery,Json,Parsing,我正在尝试使用javascript/jquery解析来自服务器的JSON响应。这是字符串: { "status": { "http_code": 200 }, "contents": "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nDate: Tue, 07 Feb 2012 08:14:38 GMT\r\nServer: localhost\r\n\r\n {\"response\": \"true\"} " } 我需要获得响应值

我正在尝试使用javascript/jquery解析来自服务器的JSON响应。这是字符串:

{
"status": {
    "http_code": 200
},
"contents": "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nDate: Tue, 07 Feb 2012 08:14:38 GMT\r\nServer: localhost\r\n\r\n {\"response\": \"true\"} "
}
我需要获得
响应

以下是我在POST查询的成功函数中尝试的内容:

            success: function(data) {
            var objstring = JSON.stringify(data);
            var result = jQuery.parseJSON(JSON.stringify(data));
            alert(objstring);

            var result1 = jQuery.parseJSON(JSON.stringify(result.contents[0]));
            alert(result1.response);

            alert(data.contents[0].response);

但到目前为止,我尝试过的任何操作都不会返回正确的结果“我一直得到未定义的
,但是
objstring
的内容是正确的,并且包含JSON字符串。

首先,将
数据类型设置为
JSON
。然后,
数据
将是包含指定数据的对象:

dataType: 'json',
success: function(data) {
    alert(data.status.http_code);
    alert(data.contents);
}

有关这些属性实现的更多说明,请阅读。它还可能帮助您使用Javascript控制台,因此您可以使用
console.log
,这是一个比
alert

更强大的工具。首先,将
数据类型设置为
json
。然后,
数据
将是包含指定数据的对象:

dataType: 'json',
success: function(data) {
    alert(data.status.http_code);
    alert(data.contents);
}
有关这些属性实现的更多说明,请阅读。它还可能帮助您使用Javascript控制台,因此您可以使用
console.log
,这是一个比
alert
功能强大得多的工具,请尝试以下方法:

var objstring=JSON.stringify(data);
var result=JSON.parse(objstring);

alert("http code:" + result.status.http_code);
alert("contents:" + result.contents);
试着这样做:

var objstring=JSON.stringify(data);
var result=JSON.parse(objstring);

alert("http code:" + result.status.http_code);
alert("contents:" + result.contents);

JSON响应本身似乎包含JSON内容。根据调用
jQuery.post()
方法的方式,您将获得字符串或JSON对象。对于第一种情况,使用jQuery.parseJSON()将字符串转换为JSON对象:

data = jQuery.parseJSON(theAboveMentionedJsonAsString);
接下来,在第一个
\r\n\r\n
之后获取
contents
属性中的所有内容:

var contentBody = data.contents.split("\r\n\r\n", 2)[1];
最后,将该字符串转换为JSON并获得所需的值:

var response = jQuery.parseJSON(contentBody).response;
// "true"

JSON响应本身似乎包含JSON内容。根据调用
jQuery.post()
方法的方式,您将获得字符串或JSON对象。对于第一种情况,使用jQuery.parseJSON()将字符串转换为JSON对象:

data = jQuery.parseJSON(theAboveMentionedJsonAsString);
接下来,在第一个
\r\n\r\n
之后获取
contents
属性中的所有内容:

var contentBody = data.contents.split("\r\n\r\n", 2)[1];
最后,将该字符串转换为JSON并获得所需的值:

var response = jQuery.parseJSON(contentBody).response;
// "true"

这无助于获取响应值,是转义码阻止我读取它。这不起作用:data.contents。response@danielbeard我没看到。我的天哪,这是一个荒谬的API。这无助于获取响应值,是转义码阻止我读取它。这不起作用:data.contents。response@danielbeard我没看到。我的天哪,这是一个荒谬的API。你在多个浏览器中测试过它,但仍然收到相同的“未定义”字符串吗?你在多个浏览器中测试过它,并且仍然收到相同的“未定义”字符串吗?