标签:
http://www.cnblogs.com/lxsweat/p/4372508.html
其中的使用方法和UserInfo实体对象就不贴代码了。
/// <summary>
/// 把对象转成json字符串
/// </summary>
/// <param name="o">对象</param>
/// <returns>json字符串</returns>
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);
}
/// <summary>
/// 把json字符串转成实体对象
/// </summary>
/// <typeparam name="T">对象</typeparam>
/// <param name="data">json字符串</param>
public static T DeserializeToModel<T>(string data)
{
return JsonConvert.DeserializeObject<T>(data);
}
/// <summary>
/// 将json字符串转换成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);
}
注意:将json list字符串转换成list<T>的时候可能会出问题。Look下面的modelUser_InfoList是一个实体列表list,然而前面的code和msg属于自定义字段。
调用方法:DeserializeToList<T>(string data) 记得取出data里面的数组传入参数
return JsonUtils.SerializeToJson(new
{
code = "1",
msg = "success",
data = modelUser_InfoList
});
示例:
JObject obj = JObject.Parse(json_list);
string json = obj["data"].ToString();
List<User_Info> listGetResultInfo = JsonUtils.DeserializeToList<User_Info>(json);
原文地址:http://www.cnblogs.com/lxsweat/p/4372508.html
转载--文章(感谢米粒儿博主分享) 关于 Json.net序列化时间问题
标签:
原文地址:http://www.cnblogs.com/workcn/p/4389177.html