views:

62

answers:

4

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 javascript object (JSON)?

{"response": {
   "success": "The activity has been removed",
   "message": "0"

  }
}

I am not using jquery.

+1  A: 

Use the JSON library?

json.org

source

mway
+1  A: 

If you use jQuery, JSON.parse(this.responseString); or jQuery.parseJSON(this.responseString); should work.

knepe
A: 

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.

Pointy
target users are blackberry devices. Most libraries don't work (well or at all) with older BB. I am trying XUI, but have not figured out it's JSON capabilities yet.
robert