2

I have a JSON string that I would like to convert to a javascript array. How can I do this?

I was under the impression thot JQuery could do this

3
  • It's not going to be possible to answer this definitively without seeing the string itself. Commented May 23, 2011 at 15:56
  • are you saying that it is not possible to parse all properly formatted JSON strings to a Javascript array? (or do you mean that some JSON strings are objects, and not convertable to an array?) Commented May 23, 2011 at 16:18
  • A JSON expression can be an object or an array. If it's an object, like { "x": "y" }, how exactly do you want to "convert" that to an array? You can parse it, and then store the object in an array if you want to, I guess, but without more explanation it's hard to know exactly what you're trying to achieve. Commented May 23, 2011 at 16:36

2 Answers 2

4

json2.js is the usual choice for this. It has decent error checking and will use native browser decoding if available.

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

Comments

1

in general you can do JSON.parse(jsonString). Most browsers support this.

with jquery, if you tell $.ajax() that the response type is json, it will do this for you. See the 'dataType' option here http://api.jquery.com/jQuery.ajax/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.