json = "{ elements: [ {type: 'radiogroup', choices: ['Yes','No','Maybe','Never'], isRequired: true, name: 'Test_01', title: 'Do you like ice cream?' }],showQuestionNumbers: 'off'}";
json = JSON.stringify(json);
model = new Model(json);
它不识别带有引号的 json 对象
var json = "{ elements: [ {type: 'radiogroup', choices: ['Yes','No','Maybe','Never'], isRequired: true, name: 'Test_01', title: 'Do you like ice cream?' }],showQuestionNumbers: 'off'}";
删除引号时有效
json = { elements: [ {type: 'radiogroup', choices: ['Yes','No','Maybe','Never'], isRequired: true, name: 'Test_01', title: 'Do you like ice cream?' }],showQuestionNumbers: 'off'};
我试过 JSON.parse(json);它不起作用,有人可以建议解决方案吗?
最佳答案
如果字符串中有 JSON,那么您应该执行 JSON.parse
,而不是 JSON.stringify
。
此外,单引号在 JSON 中是无效的,因此始终使用双引号。
// I fixed the JSON
var json = '{ elements: [ {type: "radiogroup", choices: ["Yes","No","Maybe","Never"], isRequired: true, name: "Test_01", title: "Do you like ice cream?" }],showQuestionNumbers: "off"}';
// The thing you would expect
var obj = JSON.parse(json);
关于javascript - React 中的 JSON 对象解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44955655/