2017-08-29 28 views
0

JSON.parse( '{..}')で解析しようとするとJSONコードが発生します。 JavaScriptの に私はmatching.Pleaseは私が事前にit.Thanksを是正助けるエラーがブラケットであると考え、 "ILLEGAL予期しないトークン"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 ]}');

エラーを表示します。

+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と呼ばれています、それをあなたが、中でもことができます複数行の文字列を書く

また\を使用して、通常の文字列に改行をエスケープすることができます:あなたは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 
`; 
関連する問題