convert string to json array
I get this response from an Ajax request. Javascript seems to intepret it as a string. (When I say alert this.responseText, the whole string is shown)
How can i convert it to a javascr开发者_开发问答ipt object (JSON)?
{"response": {
"success": "The activity has been removed",
"message": "0"
}
}
I am not using jquery.
If you use jQuery, JSON.parse(this.responseString);
or jQuery.parseJSON(this.responseString);
should work.
It's not the safest thing in the world, but you can do this:
var value = null, txt = this.responseText;
eval("value = (" + txt + ")");
It might be a little safer to do:
var value = null, txt = this.responseText;
!function(window) { eval("value = (" + txt + ")"); }();
but there are still all sorts of potential hacks. You're better off using a library.
Use the JSON library?
json.org
source
精彩评论