2017-08-29 88 views
0

我試圖用JSON.parse('{..}')解析JSON代碼;在JavaScript 顯示錯誤「意外令牌非法」JSON解析我的JSON對象時出錯

var obj= JSON.parse('{ 
 
\t "single": [ 
 
\t {"id": "1","question": "this is a question1?","option": ["option1","option2","option3","option4"]}, 
 
\t {"id": "2","question": "this is a question2?","option": ["option1","option2","option3","option4"]}, 
 
\t {"id": "3","question": "this is a question3?","option": ["option1","option2","option3","option4"]}, 
 
\t {"id": "4","question": "this is a question4?","option": ["optionu1","optionu2","optionu3","optionu4"]} 
 
\t ], 
 
\t "multiple": [ 
 
\t {"id": "1","question": "this is a multiple question1?","option": ["optionm1","option2lj","option3","option4"]}, 
 
\t {"id": "2","question": "this is a multiple question2?","option": ["optionm1","option2j","option3","option4"]}, 
 
\t {"id": "3","question": "this is a multiple question3?","option": ["optionm1","option2gg","option3","option4"]}, 
 
\t {"id": "4","question": "this is a multiple question4?","option": ["optionm1","option2h","option3","option4"]} 
 
\t ], 
 
\t "integer": [ 
 
\t {"id": "1","question": "this is a int question1?"}, 
 
\t {"id": "2","question": "this is a int question2?"}, 
 
\t {"id": "3","question": "this is a int question3?"}, 
 
\t {"id": "4","question": "this is a int question4?"} 
 
\t ]}');

我認爲錯誤是在支架matching.Please幫我糾正it.Thanks提前。

+0

你可以使用反引號代替'''字符嗎? – Hackerman

+1

其他然後在字符串中間的換行符,對我來說似乎很好 – adeneo

+0

我猜測證明是在布丁 - > https://jsfiddle.net/9nrx7nsk/ – adeneo

回答

2

使用重音符號,而不是'

var obj = JSON.parse(`{ 
"single": [ 
    {"id": "1","question": "this is a question1?","option": ["option1","option2","option3","option4"]}, 
    {"id": "2","question": "this is a question2?","option": ["option1","option2","option3","option4"]}, 
    {"id": "3","question": "this is a question3?","option": ["option1","option2","option3","option4"]}, 
    {"id": "4","question": "this is a question4?","option": ["optionu1","optionu2","optionu3","optionu4"]} 
    ], 
    "multiple": [ 
    {"id": "1","question": "this is a multiple question1?","option": ["optionm1","option2lj","option3","option4"]}, 
    {"id": "2","question": "this is a multiple question2?","option": ["optionm1","option2j","option3","option4"]}, 
    {"id": "3","question": "this is a multiple question3?","option": ["optionm1","option2gg","option3","option4"]}, 
    {"id": "4","question": "this is a multiple question4?","option": ["optionm1","option2h","option3","option4"]} 
    ], 
    "integer": [ 
    {"id": "1","question": "this is a int question1?"}, 
    {"id": "2","question": "this is a int question2?"}, 
    {"id": "3","question": "this is a int question3?"}, 
    {"id": "4","question": "this is a int question4?"} 
    ] 
}`); 
console.log(obj); 

這就是所謂的template strings,有了它,你可以.A。編寫多行字符串。

您也可以使用\逃避正常的字符串新行:如果您在ES6編寫

var line = '\ 
    many\ 
    lines\ 
'; 

使用模板字符串:

var obj = JSON.parse('{\ 
"single": [\ 
    {"id": "1","question": "this is a question1?","option": ["option1","option2","option3","option4"]},\ 
    {"id": "2","question": "this is a question2?","option": ["option1","option2","option3","option4"]},\ 
    {"id": "3","question": "this is a question3?","option": ["option1","option2","option3","option4"]},\ 
    {"id": "4","question": "this is a question4?","option": ["optionu1","optionu2","optionu3","optionu4"]}\ 
    ],\ 
    "multiple": [\ 
    {"id": "1","question": "this is a multiple question1?","option": ["optionm1","option2lj","option3","option4"]},\ 
    {"id": "2","question": "this is a multiple question2?","option": ["optionm1","option2j","option3","option4"]},\ 
    {"id": "3","question": "this is a multiple question3?","option": ["optionm1","option2gg","option3","option4"]},\ 
    {"id": "4","question": "this is a multiple question4?","option": ["optionm1","option2h","option3","option4"]}\ 
    ],\ 
    "integer": [\ 
    {"id": "1","question": "this is a int question1?"},\ 
    {"id": "2","question": "this is a int question2?"},\ 
    {"id": "3","question": "this is a int question3?"},\ 
    {"id": "4","question": "this is a int question4?"}\ 
    ]\ 
}'); 
console.log(obj); 
2

使用\逃脫行尾

var line = ` 
    many 
    lines 
`;