我有一个项目,我需要有两个单独的 JSON 列表,这意味着我不能在我的控制器中使用 JsonResult,所以我需要解析客户端。我使用以下内容将我的列表解析为 Json,然后从Newtonsoft.Json包中将字符串发送到我的视图模型中:
Model.JsonAllActiveItems = JsonConvert.SerializeObject(Model.AllActiveItems,Formatting.None);
然后在我的脚本文件中说:
function setActiveList(list) {
alert(list)//for test
activeList = JSON.parse(list);
};
解析命令后,我不断收到错误“Uncaught SyntaxError: Unexpected token o”
如果它有帮助的话,JSON字符串如下:
[{"ProductCompositions":[],"ImageName":"Item1","Version":0,"StatusInt":0,"Status":0,"Id":1},{"ProductCompositions":[],"ImageName":"Item2","Version":0,"StatusInt":1,"Status":0,"Id":2},{"ProductCompositions":[],"ImageName":"Item3","Version":0,"StatusInt":1,"Status":0,"Id":4},{"ProductCompositions":[],"ImageName":"Item2","Version":0,"StatusInt":0,"Status":0,"Id":5},{"ProductCompositions":[],"ImageName":"Item4","Version":0,"StatusInt":0,"Status":0,"Id":6},{"ProductCompositions":[],"ImageName":"Item4","Version":0,"StatusInt":0,"Status":0,"Id":7}]
有任何想法吗?干杯
更新:奇怪的是,在 Chrome javascript 调试器中,“list”甚至在 parse 命令之前就已经正确填充了正确的数据。但是,当我尝试在不使用 JSON.parse() 的情况下对其进行测试时,如下所示,我得到一个空字符串,例如 list is null:
var myString = "";
for (var i = 0; i < list.length; i++ ) {
myString += "<li>" + list[i].ImageName + "</li>";
}
document.getElementById("selected-items-comp").innerHtml = myString;