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 } }
标签:return,list,json,var,new,null,row From: https://www.cnblogs.com/chxl800/p/17785231.html