这是我们想要使用jquery推方括号的json ..
{"ProductionAnticipationId":{"title":"","type":"hidden","required":true,"options":"","defaultValue":""},"ProductionType": {"title": "Production Type","type": "Combobox","required": true,"options": "/BgtProduction/GetProductionType","unique": true},"Grade": {"title": "Grade","type": "Combobox","required": true,"options": "/BgtProduction/GetGrades","unique": true},"Quantity": {"title": "Quantity ","type": "","required": true,"align":"right","defaultValue": "0"} },{"Quantity":"total"}
以下是我们用来将方括号推到JSON以上的代码..
var ar = [];
if (!$.isArray(l_schemaString))
ar = ar.push($.parseJSON(l_schemaString));
else
ar = $.parseJSON(l_schemaString);
在View中应用此代码后,在控制台中显示错误, 未捕获的SyntaxError:意外的令牌,
请提供解决方案以解决此问题
答案 0 :(得分:0)
我将您的JSON粘贴到格式化程序中:http://jsonformatter.curiousconcept.com/
结果是:错误:多个JSON根元素
{
"ProductionAnticipationId":{
"title":"",
"type":"hidden",
"required":true,
"options":"",
"defaultValue":""
},
"ProductionType":{
"title":"Production Type",
"type":"Combobox",
"required":true,
"options":"/BgtProduction/GetProductionType",
"unique":true
},
"Grade":{
"title":"Grade",
"type":"Combobox",
"required":true,
"options":"/BgtProduction/GetGrades",
"unique":true
},
"Quantity":{
"title":"Quantity ",
"type":"",
"required":true,
"align":"right",
"defaultValue":"0"
}
}, <-- the problem is here
{
"Quantity":"total"
}
看起来问题是JSON中有多个对象,而不仅仅是一个。因此,当服务器写出它时,它应该被写为[ ... objects here ... ]
,以便它们被包装在一个数组中。
在其他代码示例中,这里存在一个问题:
ar = ar.push($.parseJSON(l_schemaString));
push method会返回数组的新长度,因此ar
最终会被设置为1
而不是数组,这会导致以后出现问题。