3
MyClass theSession = new MyClass() {
    accountId = 12345,
    timeStamp = DateTime.Now,
    userType = "theUserType"
};

System.Web.Script.Serialization.JavaScriptSerializer Json = new System.Web.Script.Serialization.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"

?

George Netu
  • 2,758
  • 4
  • 28
  • 49
Chris McCall
  • 10,317
  • 8
  • 49
  • 80

3 Answers3

2

You need to make a JavaScriptConverter class and register it using the RegisterConverters method.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

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/

Sebastian Markbåge
  • 3,275
  • 1
  • 18
  • 9
0

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

Community
  • 1
  • 1
nikib3ro
  • 20,366
  • 24
  • 120
  • 181