程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 利用Newtonsoft.Json實現Json序列化與反序列化,newtonsoft反序列化

利用Newtonsoft.Json實現Json序列化與反序列化,newtonsoft反序列化

編輯:C#入門知識

利用Newtonsoft.Json實現Json序列化與反序列化,newtonsoft反序列化


    在項目中用到了Newtonsoft.Json來實現序列化和反序列化,在這裡寫下實現代碼。

    1、創建類用於排除不序列化的屬性

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));
        }
    }

    2、序列化方法

    以下分別是將對象、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

    3、反序列化

    以下是將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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved