I have following 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}"
]}
The result is generated by selecting some checkboxes (KendoUI tree view witch checkbox) in client side by users:
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");
}
});
});
In server side I'm using this action method for parsing the json result:
[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");
}
But when I click on save button I'm getting this exception:
{"Cannot access child value on Newtonsoft.Json.Linq.JValue."}
Any idea?
Each element in your array is just another string - so you need to parse each of those as well. For example:
IList<Page> pages = jsonArray
.Select(text => JObject.Parse(text))
.Select(p => new Page { ... })
.ToList();
Alternatively, change your Javascript code to avoid the double-encoding, so you end up with JSON of:
{[
{"Id":3,"ParentName":"TicketController",""ChildName"":null,"Body":"Ticket","ParentId":null,"HasChildren":true,"imageUrl":null},
...
]}
See more on this question at Stackoverflow