Forcing Json Serializer to create a specific date and time format (yyyy-mm-ddTh: mm: s .msmsmsZ)

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));

It produces:

{"accountId":12345,"timeStamp":"\/Date(1268420981135)\/","userType":"theUserType"}

How can I present the date as:

"timestamp":"2010-02-15T23:53:35.963Z"

?

+3
source share
3 answers

Even if you implement JavaScriptConverter, you will have to wrap the string in an object. Fortunately, a hacker is described here:

http://blog.calyptus.eu/seb/2011/12/custom-datetime-json-serialization/

+1
source

You need to create a class JavaScriptConverterand register it using RegisterConverters.

+2
source

( ) ServiceStack.Text - 30 , . :

ASP.NET MVC Json DateTime UTC

0

Source: https://habr.com/ru/post/1736709/


All Articles