. net--processing of DateTime in serialization and deserialization

Source: Internet
Author: User
Tags string to json
to take on this, now I'm adding a date-time Type field to the class:

Public  class Student    {public       string StudentID {get; set;}       public string Name {get; set;}       public int Age {get; set;}       public string Address {get; set;}       Public DateTime Birthday {get; set;}    }

This problem occurs after serialization if the type of birthday is not processed:

{"Address": "Chaoyang District of Beijing", "Birthday": "\/date (1434943392228+0800)/", "Name": "paddy field such as Ya", "StudentID": "" "," Age ": 20}

Now let's make an improvement on the original method:

 public static class JsonConvert {//<summary>//Converts the obj to JSON. </summary>//<typeparam name= "T" ></typeparam>//<param name= "T" >the t.</p aram>//<returns>System.String.</returns>//<remarks>editor:v-liuhch createtime:201 5/6/21 21:40:55</remarks> public static string convertobjtojson<t> (T t) {Datacontrac            Tjsonserializer ser = new DataContractJsonSerializer (T.gettype ()); try {using (MemoryStream ms=new MemoryStream ()) {SE                        R.writeobject (ms,t); String Strjson=encoding.utf8.getstring (Ms.                        ToArray ());                        Replace the JSON Date string with string p = @ "\\/date\ ((\d+) \+\d+\) \\/";                     MatchEvaluator matchevaluator = new MatchEvaluator (convertjsondatatodatestring);   Regex reg = new regex (p); Strjson = Reg.                        Replace (Strjson, MatchEvaluator);                     return Strjson;        }} catch (IOException) {//Handle exception yourself return null;        }}///<summary>//Jsons the deserialize. </summary>//<typeparam name= "T" ></typeparam>//<param name= "Strjson" >the ST R json.</param>//<returns> ' 0.</returns>//&LT;REMARKS&GT;EDITOR:V-LIUHCH Createtime: 2015/6/21 21:46:37</remarks> public static T jsondeserialize<t> (String strjson) where T:clas S//constraint T is class, can also be relaxed type, here is just an example, write fun, Aubagne, here can be changed {datacontractjsonserializer ser = new Datacontractjsonseri            Alizer (typeof (T));  try {//Convert the string "Yyyy-mm-dd HH:mm:ss" to "\/date (1294499956278+0800) \ \" Format "string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2} ";                MatchEvaluator matchevaluator = new MatchEvaluator (convertdatestringtojsondate);                Regex reg = new regex (p); Strjson = Reg.                Replace (Strjson, MatchEvaluator); using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (Strjson))) {T obj = se                    R.readobject (MS) as T;                return obj;            }} catch (IOException e) {//Handle exception yourself return null;        }}///<summary>//Converts the JSON data to date string. </summary>//<param name= "M" >the m.</param>//<returns>system.string.</re turns>//<remarks>editor:v-liuhch CREATETIME:2015/6/22 11:39:18</remarks> private Static St Ring convertjsondatatodatestring (Match m) {string result = string. EmPty            DateTime dt = new DateTime (1970, 1, 1); DT = dt. Addmilliseconds (Long. Parse (M.groups[1].            Value)); DT = dt. ToLocalTime ();//Convert to local time result = dt.            ToString ("Yyyy-mm-dd HH:mm:ss");                return result;        }//<summary>//Converts the date string to JSON date. </summary>//<param name= "M" >the m.</param>//<returns>system.string.</re turns>//<remarks>editor:v-liuhch CREATETIME:2015/6/22 11:39:15</remarks> private Static St Ring Convertdatestringtojsondate (Match m) {string result = string.             Empty; DateTime dt = DateTime.Parse (M.groups[0].             Value); DT = dt.              ToUniversalTime ();            TimeSpan ts = dt-datetime.parse ("1970-01-01"); result = String. Format ("\\/date ({0}+0800) \\/", TS.            TotalMilliseconds);                return result; }              }

Of course, at the time of serialization, we can not do a separate date and time processing, get the foreground JS, then the data for an integrated filtering, so that the dead date time will not be fixed return format, it feels better than in the background processing.

The above is the. net--serialization and deserialization in the processing of the date time, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.