Coercing Json Serializer into producing a particular datetime format (yyyy-mm-ddThh:mm:ss.msmsmsZ)
MyClass theSession = new MyClass() {
accountId = 12345,
timeStamp = DateTime.Now,
userType = "theUserType"
};
System.Web.Script.Serialization.JavaScriptSerializer Json = new System.Web.Script.Se开发者_开发百科rialization.JavaScriptSerializer();
Response.Write(Json.Serialize(theSession));
Produces:
{"accountId":12345,"timeStamp":"\/Date(1268420981135)\/","userType":"theUserType"}
How can I present the date as:
"timestamp":"2010-02-15T23:53:35.963Z"
?
You need to make a JavaScriptConverter
class and register it using the RegisterConverters
method.
Even if you implement a JavaScriptConverter you would have to wrap the string in an object. Fortunately there's a hack around it described here:
http://blog.calyptus.eu/seb/2011/12/custom-datetime-json-serialization/
I recommend that you (and everybody else with this problem) just switch to ServiceStack.Text library - it's like 30 seconds to integrate and you'll solve bunch of other problems. Take a look at this question posted & answered by me:
ASP.NET MVC Json DateTime Serialization conversion to UTC
精彩评论