0

所以我有这个对象:

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 个项目的数组(它确实如此)。第一个项目不应该有任何 ChildEntities,但第二个和第三个项目应该有另一个 JournalItems 列表作为它们的 ChildEntities,但它们也都是空的。

如何获得这些序列化程序之一以将 json 数组反序列化为列表?我可以这样做吗?

4

1 回答 1

4

如果您更改ChildEntitiesChildEntitites(您的 json 字符串包含此内容) ,它应该可以工作

Json.net 和 JavaScriptSerializer 都可以工作。

于 2012-05-23T17:08:11.773 回答