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
James
26.1k17 gold badges55 silver badges79 bronze badges
3 Answers 3
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
KARASZI István
31.6k9 gold badges107 silver badges133 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Shamim Hafiz - MSFT
JSON.parse will return an Array object?
KARASZI István
Yes it does, if the provided string is an encoded array which now is.
James
My issue come from IE6. Fixed it as soon as I got you hint.
If you use jQuery, you can get it as array by specifying the dataType as Json. See jQuery.getJSON()
3 Comments
James
Same as in Mootools and includes support for IE6
RobG
Cool, include a 90k script to do something that can be done with built-in functions.
Technowise
I meant to say if they are using jQuery. And jQuery is not 90k, just 31KB (minified).
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
Guffa
703k112 gold badges760 silver badges1k bronze badges
Comments
lang-js
var x = '[1,2,3]';alert(eval(x));. That's how it was done in pre-JSON browsers.