[英]In javascript how to convert string to array and array to string
[英]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.