我有一个阵列:
var myArray = [
{question: "Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:0},
{question: "What is the capital of UK?",
choices: ["Paris", "Washington", "London", "Liverpool"],
correctAnswer:2}];
我将其字符串化后。它看起来像这样:
JSON.stringify(myArray)
"[
{
"question":"Who is Prime Minister of the United Kingdom?",
"choices":[
"David Cameron","Gordon Brown",
"Winston Churchill",
"Tony Blair"
],
"correctAnswer":0
},
{
"question":"What is the capital of UK?",
"choices":[
"Paris",
"Washington",
"London",
"Liverpool"
],
"correctAnswer":2
}
]"
哪个是无效的JSON。如何使其有效?
答案 0 :(得分:5)
只需从两个远端删除"
个字符即可。
但请注意,它们不是JSON.stringify
输出的一部分,开发人员控制台会将添加到显示,以指示输出是字符串。这意味着只有在从控制台复制/粘贴输出时才需要删除它们。