Solve the Newtonsoft. Json serialization date problem, newtonsoft. json

Source: Internet
Author: User

Solve the Newtonsoft. Json serialization date problem, newtonsoft. json

 

Code on

The usage method and the UserInfo object do not post code.

/// <Summary> /// convert the object to a json string // </summary> /// <param name = "o"> Object </param> /// <returns> json string </returns> public static string SerializeToJson (object data) {return JsonConvert. serializeObject (data );}
Expected result (in red)

Var data = {"dbstring": "test", "modelUserInfo": {"User_ID": 1002, "User_Name": "lx99959@qq.com", "Real_Name": "lx99959@qq.com ", "Password": "96e79218965eb72c92a549dd5a330112", "Mobile": "13121893780", "Email": "", "IDCard": "", "Source": 1, "Lat ": 34.2627525, "Lng": 117.19381, "Precision": 0, "Weixin_OpenID": "", "Role_Code": "[Master]", "Status": 0, "Login_Count": 19, "LastLogin_Date": "/Date (1417165362307)/", "LastLogin_IP": "127.0.0.1", "Create_Date": "/Date (1417104000000 )/", // The time will be in this format "Create_IP": "127.0.0.1 "}}
Method Optimization

 

Added overload parameters to the SerializeObject method.

Var timeConverter = new IsoDateTimeConverter {DateTimeFormat = DateTimeFormats };

 

        public static string SerializeToJson(object data, string DateTimeFormats = "yyyy-MM-dd HH:mm:ss")        {            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = DateTimeFormats };            return JsonConvert.SerializeObject(data, Formatting.Indented, timeConverter);        }

 

Other methods
/// <Summary> /// convert the json string to an object // </summary> /// <typeparam name = "T"> Object </typeparam> // /<param name = "data"> json string </param> public static T DeserializeToModel <T> (string data) {return JsonConvert. deserializeObject <T> (data );} /// <summary> /// convert the json string to list <T> /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "data"> </param> // <returns> </returns> public static List <T> DeserializeToList <T> (string data) {return JsonConvert. deserializeObject <List <T> (data );}

Note: problems may occur when converting a json list string to a list <T>. ModelUser_InfoList under Look is a list of entities. However, the previous code and msg are custom fields.

Call method: DeserializeToList <T> (string data). Remember to retrieve the array input parameters in data.

 

           return JsonUtils.SerializeToJson(new            {                code = "1",                msg = "success",                data = modelUser_InfoList            });

Example:

JObject obj = JObject.Parse(json_list);string json = obj["data"].ToString();List<User_Info> listGetResultInfo = JsonUtils.DeserializeToList<User_Info>(json);

 

 

Address: http://www.cnblogs.com/lxsweat/p/4372508.html

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.