Deserializing Nested Object

Jan 3, 2011 at 5:15 PM


I am new to JSON.NET.

I have a class called TaskMessage that has a member variable, called sentObject, that is of type object. This member variable will be used to store any .NET object that needs to be sent to the server.

I am setting the task.sentObject member with a variable of type BankVO and serialing TaskMessage like this:

task = new TaskMessage();
BankVO bank = new BankVO(); = 1;
task.sentObject = (object) bank;
message = JsonConvert.SerializeObject( task );

When task is received by my server I deserialize it like this:
TaskMessage task = JsonConvert.DeserializeObject<TaskMessage>( message);

At the server the nested object stored in sentObject is of Type JObject. How can I convert it back to BankVO. When I try to do It I receive the error message: {"Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Wpf.BanKVO'."}

Can you Help me.
Thanks in advance

Feb 3, 2011 at 7:50 AM

I'm facing the same type of problem. Casting what's returned from the deserializer does not work for items inside the collections of my object.

obj = (T)serializer.Deserialize(reader, type);

Any insight would be helpful.

Feb 5, 2011 at 7:29 AM
Edited Feb 7, 2011 at 2:00 PM

I have the same problem.

Try this


string output = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple});

Person deserializedPerson = JsonConvert.DeserializeObject<Person>(output, new JsonSerializerSettings{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple});


Json.NET 3.5 Release 8 – 3.5 Final - James Newton-King

c# - Serializing/Deserializing Dictionary of objects with JSON.NET - Stack Overflow