0
我具有以下JSON:JSON.NET:從JSON反序列化與LINQ
{[
"{\"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");
}
但WH我點擊保存按鈕我得到這個例外:
{"Cannot access child value on Newtonsoft.Json.Linq.JValue."}
任何想法?
*的ParentId =(int)的P [ 「的ParentId」] *似乎腥......我看到至少有一個元素* ParentId * * null *。 – xanatos