Json数据时间格式的处理
方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using Newtonsoft.Json; 2 using Newtonsoft.Json.Converters;//需引入Newtonsoft.Json.dll 3 public class ConvertHelper 4 { 5 /// <summary> 6 /// 将对象转成json格式并格式化日期:yyyy-MM-dd 7 /// </summary> 8 /// <param name="jsonObject"></param> 9 /// <param name="dateType"></param> 10 /// <returns></returns> 11 public static string BuildJsonDateString(object jsonObject, string dateType = "yyyy-MM-dd") 12 { 13 var timeConverter = new IsoDateTimeConverter { DateTimeFormat = dateType }; 14 return JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented, timeConverter); 15 } 16 17 /// <summary> 18 /// 将对象转成json格式并格式化时间:yyyy-MM-dd HH:mm:ss 19 /// </summary> 20 /// <param name="jsonObject"></param> 21 /// <param name="dateType"></param> 22 /// <returns></returns> 23 public static string BuildJsonDateTimeString(object jsonObject, string dateType = "yyyy-MM-dd HH:mm:ss") 24 { 25 var timeConverter = new IsoDateTimeConverter { DateTimeFormat = dateType }; 26 return JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented, timeConverter); 27 } 28 }
调用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public string GetList() 2 { 3 News_BLL bll = new News_BLL(); 4 DataTable dt = bll.GetList("").Tables[0]; 5 var list = Common.ConvertHelper<Model.News_Model.News_ITM>.ConvertToList(dt); 6 return Common.ConvertHelper.BuildJsonDateTimeString(list); 7 }