开发者

convert string to json array

开发者 https://www.devze.com 2023-01-19 11:36 出处:网络
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)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消