簡體   English   中英

如何將此字符串轉換為javascript數組?

[英]How to convert this string to a javascript array?

當我嘗試JSON.parse這個字符串"['Answer 1', 'Answer 2']"時出現這個錯誤

Uncaught SyntaxError: Unexpected token ' in JSON at position 1
    at Object.parse (<anonymous>)
    at <anonymous>:1:6

誰能告訴我怎么做?

您需要將所有單引號 ( ' ) 替換為雙引號 ( " ) 以解析為 javascript 數組。

 const input = "['Answer 1', 'Answer 2']"; const output = JSON.parse(input.replaceAll("'", '"')); console.log(output);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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