Tuesday, August 16, 2011

MVC2 Ajax and Date

With .NET MVC2/3, you can serialize an object into JSON easily by using the JSON(object) function. However, there is a minor issue you may want to watch out. In .NET, DateTime object is serialized as an signed long integer of the milliseconds sing the January 1, 1970. If you use the normal eval() method to convert the JSON string into objects, you will get the date evaluated wrong.

Instead you should use the Sys.Serialization.JavaScriptSerializer.deserialize() function (as below) instead.

var a = Sys.Serialization.JavaScriptSerializer.deserialize(context.get_data());




Also see: http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx

No comments: