我跟随json:
{[
"{\"Id\":3,\"ParentName\":\"TicketController\",\"ChildName\":null,\"Body\":\"Ticket\",\"ParentId\":null,\"HasChildren\":true,\"imageUrl\":null}",
"{\"Id\":0,\"ParentName\":\"TicketController\",\"ChildName\":\"MainPage\",\"Body\":\"HomePage\",\"ParentId\":3,\"HasChildren\":false,\"imageUrl\":null}",
"{\"Id\":1,\"ParentName\":\"TicketController\",\"ChildName\":\"TicketList\",\"Body\":\"Requests\",\"ParentId\":3,\"HasChildren\":false,\"imageUrl\":null}",
"{\"Id\":2,\"ParentName\":\"TicketController\",\"ChildName\":\"Responses\",\"Body\":\"Responses\",\"ParentId\":3,\"HasChildren\":false,\"imageUrl\":null}",
"{\"Id\":3,\"ParentName\":\"TicketController\",\"ChildName\":\"UpdateResponse\",\"Body\":\"Edit\",\"ParentId\":3,\"HasChildren\":false,\"imageUrl\":null}",
"{\"Id\":4,\"ParentName\":\"TicketController\",\"ChildName\":\"UpdateResponse\",\"Body\":\"Update\",\"ParentId\":3,\"HasChildren\":false,\"imageUrl\":null}",
"{\"Id\":5,\"ParentName\":\"TicketController\",\"ChildName\":\"RequestDetail\",\"Body\":\"Detail\",\"ParentId\":3,\"HasChildren\":false,\"imageUrl\":null}",
"{\"Id\":6,\"ParentName\":\"TicketController\",\"ChildName\":\"RequestDetail\",\"Body\":\"Save\",\"ParentId\":3,\"HasChildren\":false,\"imageUrl\":null}",
"{\"Id\":7,\"ParentName\":\"TicketController\",\"ChildName\":\"DeleteRequest\",\"Body\":\"Delete\",\"ParentId\":3,\"HasChildren\":false,\"imageUrl\":null}"
]}
通过在用户端选择一些复选框(KendoUI树视图女巫复选框)来生成结果:
var checkedNodes = [];
function checkedNodeIds(nodes) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].checked) {
var page = {
Id: nodes[i].Id,
ParentName: nodes[i].ParentName,
ChildName: nodes[i].ChildName,
Body: nodes[i].Body,
ParentId: nodes[i].ParentId,
HasChildren: nodes[i].HasChildren,
imageUrl: nodes[i].imageUrl
};
checkedNodes.push(JSON.stringify(page));
console.log(nodes[i]);
}
if (nodes[i].hasChildren) {
checkedNodeIds(nodes[i].children.view(), checkedNodes);
}
}
}
$('#btnSave').click(function() {
var tv = $("#my-treeview").data("kendoTreeView");
checkedNodeIds(tv.dataSource.view());
var data = {};
data.checks = JSON.stringify(checkedNodes);
$.ajax({
url: '@Url.Action("SaveTreeViewData", "Main", new {area = ""})',
type: 'POST',
data: data,
dataType: 'json',
success: function(result) {
alert("Success");
},
error: function(result) {
alert("Error");
}
});
});
在服务器端,我正在为parsing the json结果使用此操作方法:
[HttpPost]
public ActionResult SaveTreeViewData(string checks) {
//var result = JsonConvert.DeserializeObject<Page[]>(checks.Substring(1, checks.Length-2));
var jsonArray = JArray.Parse(checks);
IList < Page > pages = jsonArray.Select(p = > new Page {
Id = (int) p["Id"],
ParentName = (string) p["ParentName"],
ChildName = (string) p["ChildName"] ? ? "NoChild",
ParentId = (int) p["ParentId"],
HasChildren = (bool) p["HasChildren"],
Body = (string) p["Body"],
imageUrl = (string) p["imageUrl"]
}).ToList();
return Json("ok");
}
但是当我点击“保存”按钮时,我遇到了这个例外:
{"Cannot access child value on Newtonsoft.Json.Linq.JValue."}
有什么想法吗?
答案 0 :(得分:3)
数组中的每个元素都只是另一个字符串 - 所以你需要解析每个元素。例如:
IList<Page> pages = jsonArray
.Select(text => JObject.Parse(text))
.Select(p => new Page { ... })
.ToList();
或者,更改您的Javascript代码以避免双重编码,因此您最终得到JSON:
{[
{"Id":3,"ParentName":"TicketController",""ChildName"":null,"Body":"Ticket","ParentId":null,"HasChildren":true,"imageUrl":null},
...
]}