list集合,dataTable 转json null转空字符串,时间格式
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Utils { public class NewtonsoftHelper { public static string ToJson<T>(T obj) { if (obj == null) return null; return JsonConvert.SerializeObject(obj); } public static T ToObject<T>(string jsonString) { if (string.IsNullOrEmpty(jsonString)) return default(T); return JsonConvert.DeserializeObject<T>(jsonString); } #region 新加的方法 /// <summary> /// 对象转json null转空字符串,时间格式 /// </summary> /// <param name="item"></param> /// <returns></returns> public static string ItemToJson(object item) { Dictionary<string, object> row = new Dictionary<string, object>(); var propertyInfos = item.GetType().GetProperties(); foreach (var propertyInfo in propertyInfos) { object result = propertyInfo.GetValue(item); object value = result == null ? "" : result; row.Add(propertyInfo.Name, value); } var settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore,//这种方式指定忽略循环引用,是在指定循环级数后忽略,返回的json数据中还是有部分循环的数据 DateFormatString = "yyyy-MM-dd HH:mm:ss", //ContractResolver = new CamelCasePropertyNamesContractResolver(),//json中属性开头字母小写的驼峰命名 }; return JsonConvert.SerializeObject(row, settings); } /// <summary> /// list转json null转空字符串,时间格式 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <returns></returns> public static string ListToJson<T>(List<T> list) { List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); Dictionary<string, object> row; foreach (var item in list) { row = new Dictionary<string, object>(); var propertyInfos = item.GetType().GetProperties(); foreach (var propertyInfo in propertyInfos) { object result = propertyInfo.GetValue(item); object value = result == null ? "" : result; row.Add(propertyInfo.Name, value); } rows.Add(row); } var settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore,//这种方式指定忽略循环引用,是在指定循环级数后忽略,返回的json数据中还是有部分循环的数据 DateFormatString = "yyyy-MM-dd HH:mm:ss", //ContractResolver = new CamelCasePropertyNamesContractResolver(),//json中属性开头字母小写的驼峰命名 }; return JsonConvert.SerializeObject(rows, settings); } /// <summary> /// DataTable转json null转空字符串,时间格式 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string DataTableToJson(DataTable dt) { List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); Dictionary<string, object> row; foreach (DataRow dr in dt.Rows) { row = new Dictionary<string, object>(); foreach (DataColumn col in dt.Columns) { object value = dr[col]== Convert.DBNull ? "" : dr[col]; row.Add(col.ColumnName, value); } rows.Add(row); } var settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore,//这种方式指定忽略循环引用,是在指定循环级数后忽略,返回的json数据中还是有部分循环的数据 DateFormatString = "yyyy-MM-dd HH:mm:ss", //ContractResolver = new CamelCasePropertyNamesContractResolver(),//json中属性开头字母小写的驼峰命名 }; return JsonConvert.SerializeObject(rows, settings); } #endregion } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2019-10-24 Vs2017 FrameWork EF Mysql Mvc 三层整合1