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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
robert
  • 625
  • 3
  • 12
  • 23

3 Answers3

16

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

knepe
  • 325
  • 1
  • 3
  • 9
3

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
  • 405,095
  • 59
  • 585
  • 614
  • 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 Oct 06 '10 at 17:52
2

Use the JSON library?

json.org

source

mway
  • 4,334
  • 3
  • 26
  • 37