在項目中用到了Newtonsoft.Json來實現序列化和反序列化,在這裡寫下實現代碼。
public class ExcludePropertiesContractResolver : DefaultContractResolver { IEnumerable<string> lstExclude; public ExcludePropertiesContractResolver(IEnumerable<string> excludedProperties) { lstExclude = excludedProperties; } protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { return base.CreateProperties(type, memberSerialization).ToList().FindAll(p => !lstExclude.Contains(p.PropertyName)); } }
以下分別是將對象、DataTable、集合序列化為Json字符串的方法:
/// <summary> /// T對象轉換成json字符串 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static string GetJsonString<T>(T obj, params string[] ignoreFields) { IsoDateTimeConverter iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-ddTHH:mm:ss"; iso.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal; string jsonStr = string.Empty; JsonSerializerSettings js = new JsonSerializerSettings(); if (ignoreFields != null && ignoreFields.Length > 0) { List<string> ignoreList = ignoreFields.ToList(); js.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; js.ContractResolver = new ExcludePropertiesContractResolver(ignoreList); } js.Converters.Add(iso); jsonStr = JsonConvert.SerializeObject(obj, Formatting.Indented, js); return jsonStr; } /// <summary> /// DataTable對象轉換成json字符串 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetJsonString(DataTable dt, params string[] ignoreFields) { IsoDateTimeConverter iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-ddTHH:mm:ss"; iso.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal; string jsonStr = string.Empty; JsonSerializerSettings js = new JsonSerializerSettings(); if (ignoreFields != null && ignoreFields.Length > 0) { List<string> ignoreList = ignoreFields.ToList(); js.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; js.ContractResolver = new ExcludePropertiesContractResolver(ignoreList); } js.Converters.Add(new DataTableConverter()); js.Converters.Add(iso); jsonStr = JsonConvert.SerializeObject(dt, Formatting.Indented,js); return jsonStr; } /// <summary> /// List對象轉換成json字符串 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetJsonString<T>(List<T> list, params string[] ignoreFields) { IsoDateTimeConverter iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-ddTHH:mm:ss"; iso.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal; string jsonStr = string.Empty; JsonSerializerSettings js = new JsonSerializerSettings(); if (ignoreFields != null && ignoreFields.Length > 0) { List<string> ignoreList = ignoreFields.ToList(); js.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; js.ContractResolver = new ExcludePropertiesContractResolver(ignoreList); } js.Converters.Add(iso); jsonStr = JsonConvert.SerializeObject(list, Formatting.Indented, js); return jsonStr; } View Code以下是將Json字符串反序列化為對象、集合的方法。
/// <summary> /// json字符串轉換成T對象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonStr"></param> /// <returns></returns> public static T GetObjectByJsonString<T>(string jsonStr) { T obj = JsonConvert.DeserializeObject<T>(jsonStr); return obj; } /// <summary> /// json字符串轉換成List對象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonStr"></param> /// <returns></returns> public static List<T> GetListByJsonString<T>(string jsonStr) { List<T> list = JsonConvert.DeserializeObject<List<T>>(jsonStr); return list; } View Code