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