简体   繁体   中英

Converting string to array in JavaScript

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?

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 .

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM