我有一个项目,我需要有两个单独的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"即使在解析命令之前,也正确填充了正确的数据。但是,当我尝试不使用JSON.parse()进行测试时,我得到一个空字符串,如list为null:
var myString = "";
for (var i = 0; i < list.length; i++ ) {
myString += "<li>" + list[i].ImageName + "</li>";
}
document.getElementById("selected-items-comp").innerHtml = myString;
答案 0 :(得分:0)
所以问题是如果你将Newtonsoft.Json dll与MVC3一起使用,你不需要在JavaScript中解析JSON。这种魔力发生在幕后。我在测试中无法得到任何东西的原因是我是一个jQuery noob并且做了
document.getElementById("selected-items-comp").innerHtml = myString;
而不是
$('#selected-items-comp').html(myString);
现在它有效。