转载--文章(感谢米粒儿博主分享) 关于 Json.net序列化时间问题
http://www.cnblogs.com/lxsweat/p/4372508.html
上代码
其中的使用方法和UserInfo实体对象就不贴代码了。
///
/// 把对象转成json字符串
///
/// 对象
/// json字符串
public static string SerializeToJson(object data)
{
return JsonConvert.SerializeObject(data);
}
得到结果(红色字体)
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)/",//时间会成这种格式
"Create_IP": "127.0.0.1"
}
}
方法优化
给SerializeObject方法加了重载参数
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);
}
其他方法
///
/// 把json字符串转成实体对象
///
/// 对象
/// json字符串
public static T DeserializeToModel(string data)
{
return JsonConvert.DeserializeObject(data);
}
///
/// 将json字符串转换成list
///
///
///
///
public static List DeserializeToList(string data)
{
return JsonConvert.DeserializeObject>(data);
}
注意:将json list字符串转换成list
调用方法:DeserializeToList
return JsonUtils.SerializeToJson(new
{
code = "1",
msg = "success",
data = modelUser_InfoList
});
示例:
JObject obj = JObject.Parse(json_list);
string json = obj["data"].ToString();
List listGetResultInfo = JsonUtils.DeserializeToList(json);
原文地址:http://www.cnblogs.com/lxsweat/p/4372508.html