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.

3
  • check out stackoverflow.com/questions/45015/… Commented Oct 6, 2010 at 17:07
  • using prototype or native javascript? Commented Oct 6, 2010 at 17:09
  • The reason might be "http status" code. Check the http status code (via F12 in IE or Firebug in FF) to see if it's 200 (=OK) or not. Commented Sep 7, 2012 at 13:35

3 Answers 3

16

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
2

Use the JSON library?

json.org

source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.