JSON to C# POCO - again!

This is really frustrating.  There are so many (bad) ways to deserialize JSON in C#.

In my previous post, we discovered that dynamic types really are not a good way to do it.

Now I have discovered that new JavaScriptSerializer().Deserialize<T>(json) will not convert datetimes properly.  Json.Decode<T>(json) has the same issue.

EDIT:

Today I tried ReadAsAsync<T>.  I originally had datetime conversion issues, but it was because my C# POCO did not have the correct property names.  If the json is not from ASP.NET and the JSON keys have underscores, your POCO property names must have underscores also.

For reference:

2 SO Questions that are the most popular:
http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object
http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object

2 Asp.NET articles that are helpful for ReadAsAsync
http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client
http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

Comments

Popular Posts