A pit in the MVC JSON method

Source: Internet
Author: User

There is a method under the MVC controller class

      //        //Summary://creates a System.Web.Mvc.JsonResult object that serializes the specified object//To JavaScript Object Notation (JSON). //        //Parameters://Data://The JavaScript object graph to serialize. //        //Returns://The JSON result object is serializes the specified object to JSON format. the//result object is prepared by this method is written to the response by the//The ASP. NET MVC framework when the object is executed.        protected InternalJsonresult Json (Objectdata);

It is generally used to return a JSON interaction with client JS (JQuery) in the action.

 Public jsonresult getrepchangetest ()        {            return  Json (myobj);        }

When you have a datetime field in the image, the JSON method serializes it into this format

" Rec_dt " " /date (1498466813000)/ ",

In the client JS, we treat it as a string, and then turn it into a JS date format, such as

functionDT2 (tm) {varTime = tm.replace (/[\ ' \ "\\\/\b\f\n\r\t)]/g,"). SUBSTRING (5); Console.log (time); //Get timestamp value            varDate =NewDate (parseint (time));//turn into a date format            //Console.log (date);                   //the following will be converted to the format you want            varmonth = Date.getmonth () + 1 < 10? "0" + (date.getmonth () + 1): Date.getmonth () + 1; varcurrentdate = Date.getdate () < 10? "0" +date.getdate (): Date.getdate (); varHH = date.gethours () < 10? "0" +date.gethours (): Date.gethours (); varmm = Date.getminutes () < 10? "0" +date.getminutes (): Date.getminutes (); vardt= date.getfullyear () + "-" + month + "-" + currentdate + "" + hh + ":" +mm;        alert (DT); }
View Code

You can revert to a date format, like 2017-06-26 16:46, and it all looks perfect, but the pit is right here.

The timestamp returned by the JSON method (in milliseconds) is based on UTC time (GMT 0), and my time zone is gmt+8, so there is a 8-hour time difference.

But the var in JS date = new Date (xxxxx); This is based on the UTC time (the test proceeds, not the drill), so the client gets the correct time. If you use C # code like this to manually verify

DateTime.Parse ("1970-1-1"). AddSeconds (1498466813)

The resulting date is just 8 hours.

The date type in MySQL has datetime, timestamp, if you use DateTime, there is no problem (although the time zone, but the client is also based on UTC restore, so can get the correct value), but when you use timestamp, The JSON method serializes the value based on the current time zone, so that the JS client is restored with the above method is wrong, added 8 hours, because the client is UTC-based, not on. This is where the real pit lies. Read from the database, C # Debugging two types of values are not different, are correct, but the entity to the image (the definition of the type is DateTime) after the Jons method conversion results are different, specifically to be studied ....

Workaround

1. Do not use Json conversion, with other, such as Newtonsoft.json, after the conversion of Jsonconvert.serializeobject (obj) date has become a date style string format, directly displayed.

 Public contentresult getRepChangeTest2 ()        {            var obj = rptdal. Reportchangelist ();             string json=jsonconvert.serializeobject (obj);             return " Application/json " );        }

2. Use a datetime type instead of timestamp in the database

A pit in the MVC JSON method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.