簡體   English   中英

在 JavaScript 中將字符串轉換為數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM