[英]Converting string to array in JavaScript
我正在向我的服務器發出異步請求,並將數組作為字符串返回。 該字符串采用正確的數組形式,例如:
"[{"spo":"I"},{"spo":"hate"},{"spo":"computers"}]"
有沒有辦法簡單地從這個字符串創建一個數組?
這是一個 JSON 字符串,您可以使用它創建一個數組:
JSON.parse('[{"spo":"I"},{"spo":"hate"},{"spo":"computers"}]')
在舊瀏覽器中,您可能需要包含json2.js 。
如果您使用 jQuery,您可以通過將 dataType 指定為 Json 來將其作為數組獲取。 見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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.