繁体   English   中英

JSON.NET:使用LINQ从JSON反序列化

[英]JSON.NET: Deserializing from JSON with LINQ

我有以下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");
        }
    });
});

在服务器端,我正在使用此操作方法来解析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."}

任何想法?

数组中的每个元素只是另一个字符串-因此您也需要解析每个元素。 例如:

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},
  ...

]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM