无法使用JSON.parse解析字符串数组

时间:2018-05-24 12:17:07

标签: javascript json

此代码:

JSON.parse("['Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness']")

返回此错误:

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

但是这段代码完美无缺:

var arr = ['Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness']

那么有比JSON更好的解析方法吗?我看到的唯一解决方案是创建自己的解析器。

我正在使用Google Chrome v 66.0.3359.181

1 个答案:

答案 0 :(得分:1)

无需编写自己的解析器。在javascript中双引号json是标准的json格式。因此,您可以在json中使用双引号并将其包装在单引号中,以便将其解析为有效的JSON

&#13;
&#13;
var arr = JSON.parse('["Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness"]');
console.log(arr);
&#13;
&#13;
&#13;

相关问题