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
415k62 gold badges600 silver badges633 bronze badges
asked Oct 6, 2010 at 17:05
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.

answered Oct 6, 2010 at 17:17
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.

answered Oct 6, 2010 at 17:31

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

answered Oct 6, 2010 at 17:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.