无法反序列化具有子列表的对象



所以我有这个对象:

public class JournalItem
{
    public string Description { get; set; }
    public bool IsShared { get; set; }
    public bool IsDeleted { get; set; }
    public bool IsGroup { get; set; }
    public Guid Id { get; set; }
    public List<JournalItem> ChildEntities { get; set; }
}

它有一个相同类型的对象列表,ChildEntitites。我有一个页面,将其中的数组作为 Json 发布回服务器。这是它返回的 Json:

[{"Description":"Develop social skills","Id":"d48749ea-2b50-4563-b47c-f2014b08c53a","IsShared":false,"IsDeleted":false,"IsGroup":false,"ChildEntitites":[]},{"Description":"Be more like Joel D.","Id":"a18749ea-2b50-4563-b47c-f2014b08d123","IsShared":true,"IsDeleted":false,"IsGroup":true,"ChildEntitites":[{"Description":"Wear cool glasses","Id":"77c56855-5626-4107-bc82-5862ccdb0943","IsShared":false,"IsDeleted":false,"IsGroup":false,"ChildEntitites":[]},{"Description":"Get 16GB of RAM","Id":"82081eab-b4ce-41fe-bcec-22178b7ed8e6","IsShared":false,"IsDeleted":false,"IsGroup":false,"ChildEntitites":[]}]},{"Description":"Nested group 1","Id":"9495f718-9e7b-4936-b1bf-112d1e7d3ef5","IsShared":false,"IsDeleted":false,"IsGroup":true,"ChildEntitites":[{"Description":"Nested group 2","Id":"70a7e919-1253-4dbd-b41b-49d8bd599657","IsShared":false,"IsDeleted":false,"IsGroup":true,"ChildEntitites":[{"Description":"Nested group 3","Id":"77bfcb11-cba2-48a9-8a1b-b4e0c9b1c5e1","IsShared":false,"IsDeleted":false,"IsGroup":true,"ChildEntitites":[{"Description":"Very nested objective","Id":"274c786b-d09c-4228-a18a-629d4ca9aed3","IsShared":false,"IsDeleted":true,"IsGroup":false,"ChildEntitites":[]}]}]}]}]

以下是我如何使用JavaScriptSerilaiazer将其转换为实际对象:

var ser = new JavaScriptSerializer();
var journalStructure = ser.Deserialize<List<JournalItem>>(journalJson);

(我也尝试过NewtonSoft,结果相同)

这应该给我一个包含 3 个项目的数组(确实如此)。第一项不应有任何子实体,但第二项和第三项应具有另一个日记项列表作为其子实体,但这些列表也均为空。

如何获取这些序列化程序之一以将 json 数组反序列化为列表?我能做到吗?

如果您将ChildEntities更改为ChildEntitites,它应该可以工作(您的 json 字符串包含这个)

Json.net 和 JavaScriptSerializer 都可以工作。

相关内容

  • 没有找到相关文章

最新更新