所以我有这个对象:
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,但这些也都是null。
如何将这些序列化程序中的一个作为列表反序列化为json数组?我能这样做吗?
答案 0 :(得分:4)
如果您将ChildEntities
更改为ChildEntitites
(您的json字符串包含此内容),它应该有效
Json.net和JavaScriptSerializer都可以工作。