Colin DeClue
Colin DeClue

Reputation: 2244

Can't deserialzie an object that has a sublist

So I have this object:

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; }
}

It has a list of the same type of object there, ChildEntitites. I've got a page that posts an array of these back to the server as Json. Here's the Json it returns:

[{"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":[]}]}]}]}]

Here's how I'm using JavaScriptSerilaiazer to conver it to an actual object:

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

(I've also tried NewtonSoft with the same result)

This should give me an array of 3 items (which it does). The first item shouldn't have any ChildEntities, but the second and third should have another list of JournalItems as their ChildEntities, but those are also both null.

How can I get one of these serializer things to deserialize the json array as a list? Can I do that?

Upvotes: 0

Views: 377

Answers (1)

L.B
L.B

Reputation: 116168

It should work if you change ChildEntities to ChildEntitites (your json string contains this)

Both Json.net and JavaScriptSerializer work.

Upvotes: 4

Related Questions