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);它没有工作,有人请建议解决方案吗?
答案 0 :(得分:3)
如果您在字符串中使用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);
答案 1 :(得分:0)
因为您正在尝试对字符串进行字符串化而没有对象删除“”并且它将成为对象然后尝试JSON.stringify并且JSON.parse不起作用,因为数据不是正确的JSON字符串格式。例如:
var object ='{“e”:“a”}'; 然后使用JSON.parse(object); 这将在对象中转换您的字符串。