How to deserialize a JObject to .NET object

Sebastian picture Sebastian · Dec 14, 2010 · Viewed 191.1k times · Source

I happily use the Newtonsoft JSON library. For example, I would create a JObject from a .NET object, in this case an instance of Exception (might or might not be a subclass)

if (result is Exception)
    var jobjectInstance = JObject.FromObject(result);

now I know the library can deserialize JSON text (i.e. a string) to an object

// only works for text (string)
Exception exception = JsonConvert.DeserializeObject<Exception>(jsontext); 

but what I am looking for is:

// now i do already have an JObject instance
Exception exception = jobjectInstance.????

Well it is clear that I can go from on JObject back to JSON text and then use the deserialize functionality, but that seems backwards to me.

Answer

Tien Do picture Tien Do · Aug 31, 2012

According to this post, it's much better now:

// pick out one album
JObject jalbum = albums[0] as JObject;

// Copy to a static Album instance
Album album = jalbum.ToObject<Album>();

Documentation: Convert JSON to a Type