Here is the JSON returned from my Server.
"[{"description":"A user","name":"test","type":"user"}]"
I want to remove the outer double quates. Which means I want the JSON as
[{"description":"A user","name":"test","type":"user"}]
How can I do this? Please help.
Here is the JSON returned from my Server.
"[{"description":"A user","name":"test","type":"user"}]"
I want to remove the outer double quates. Which means I want the JSON as
[{"description":"A user","name":"test","type":"user"}]
How can I do this? Please help.
Share Improve this question asked Aug 24, 2012 at 9:10 Thilok GunawardenaThilok Gunawardena 9245 gold badges22 silver badges44 bronze badges 3- 1 If it has quotes around it like that, it is a string of JSON and not JSON. – Quentin Commented Aug 24, 2012 at 9:14
- if your server is actually sending those outer double quotes, it's not legal JSON... – Alnitak Commented Aug 24, 2012 at 9:15
- @Quentin Thank you all. So if this is not a proper JSON, how do I convert this string of JSON to real JSON? – Thilok Gunawardena Commented Aug 24, 2012 at 9:21
5 Answers
Reset to default 4You want to turn the JSON into JS objects right? If so, you would do JSON.parse(json)
. If you need IE7 support, you have to include a polyfill for JSON as it's not supported in IE7<.
You can get the current JSON polyfill here: https://github./douglascrockford/JSON-js/blob/master/json2.js
As the response from the server is not valid JSON you have to request it as text, fix it, and parse it as JSON. Use dataType: 'text'
in your options in the ajax call.
Use the substr
method to cut of the first and last character:
data = data.substr(1, data.length - 2);
Then you can parse the JSON:
var obj = $.parseJSON(data);
To strip the first and last character from a string:
var fixed_string = string.substring(1, string.length - 1);
var str = '[{"description":"A user","name":"test","type":"user"}]';
var data = (new Function( "return " +s))(); console.log(f);
In your case response from server is a json string you have to convert it to json object
var str = '[{"description":"A user","name":"test","type":"user"}]';
str = eval('('+str+')');
console.log(str[0].name) //this will give test
Hope this helps.