2

I am making a asynchronous request to my server and its returning an array as a string. The string is in the proper array form for example:

"[{"spo":"I"},{"spo":"hate"},{"spo":"computers"}]"

Is there a way to simply create an array from this string?

Lance Roberts
22.9k32 gold badges115 silver badges132 bronze badges
asked Jul 8, 2011 at 7:59
1
  • The builtin JSON function is best if available, you can also use eval: var x = '[1,2,3]';alert(eval(x));. That's how it was done in pre-JSON browsers. Commented Jul 8, 2011 at 8:41

3 Answers 3

7

That's a JSON string, you can create an array from it with:

JSON.parse('[{"spo":"I"},{"spo":"hate"},{"spo":"computers"}]')

In older browsers you may need to include the json2.js.

answered Jul 8, 2011 at 8:02
Sign up to request clarification or add additional context in comments.

3 Comments

JSON.parse will return an Array object?
Yes it does, if the provided string is an encoded array which now is.
My issue come from IE6. Fixed it as soon as I got you hint.
3

If you use jQuery, you can get it as array by specifying the dataType as Json. See jQuery.getJSON()

answered Jul 8, 2011 at 8:03

3 Comments

Same as in Mootools and includes support for IE6
Cool, include a 90k script to do something that can be done with built-in functions.
I meant to say if they are using jQuery. And jQuery is not 90k, just 31KB (minified).
0

The string happens to correspond to the JSON format, so you can use a JSON parser to turn it into an array, for example the JSON parser in jQuery:

var myLittleArray = $.parseJSON(theJsonString);
answered Jul 8, 2011 at 8:06

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.