I am trying to parse a JSON response from a server using javascript/jquery. This is the string:
{
"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\"} "
}
I need to get the response value.
Here is what I am trying in the success function of my POST query:
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);
But so far nothing I have tried returns the correct result’ I keep getting undefined, however the contents of objstring are correct and contain the JSON string.
Seems like the JSON response itself contains JSON content. Depending on how you call the
jQuery.post()method, you’ll either get a string or a JSON object. For the first case, use jQuery.parseJSON() to convert the string to a JSON object:Next, get everything inside the
contentsproperty after the first\r\n\r\n:Lastly, convert that string to JSON and get the desired value: