.Net使用Newtonsoft.Json.dll(JSON.NET)對象序列化成json、反序列化json示例教程

JSON作爲一種輕量級的數據交換格式,簡單靈活,被很多系統用來數據交互,作爲一名.NET開發人員,JSON.NET無疑是最好的序列化框架,支持XML和JSON序列化,高性能,免費開源,支持LINQ查詢。目前已被微軟集成於webapi框架之中,因此,熟練掌握JSON.NET相當重要,這篇文章是零度參考官網整理的示例,通過這些示例,可以全面瞭解JSON.NET提供的功能。

Newtonsoft.Json的地址:

官網:http://json.codeplex.com/

源碼地址:https://github.com/JamesNK/Newtonsoft.Json

Newtonsoft.Json.dll下載:https://github.com/JamesNK/Newtonsoft.Json/releases

1、使用Newtonsoft.Json(JSON.NET)序列化對象,通過Newtonsoft.Json.Formatting將json格式化輸出。

複製代碼
            Account account = new Account
            {
                Email = "[email protected]",
                Active = true,
                CreatedDate =DateTime.Now,
                Roles = new List<string> { "User", "Admin" }
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(account, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);
複製代碼
複製代碼
    public class Account
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<string> Roles { get; set; }
    }
複製代碼

執行結果:

2、使用Newtonsoft.Json(JSON.NET)序列化List集合:

            List<string> videogames = new List<string> { "HTML5", "JavaScript", ".net","c#",".net core" };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(videogames);
            Console.WriteLine(json);

執行結果:

3、使用Newtonsoft.Json(JSON.NET)序列化dictionary字典

複製代碼
            System.Collections.Generic.Dictionary<string, string> dic = new System.Collections.Generic.Dictionary<string, string>
            {
                { "Name", "張三" },
                { "Age", "20" }, 
                { "Email", "[email protected]" }
            };
            string json1 = Newtonsoft.Json.JsonConvert.SerializeObject(dic, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json1);
            Console.WriteLine("");
            Console.WriteLine("未格式化的json:");
            string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(dic, Newtonsoft.Json.Formatting.None);
            Console.WriteLine(json2);
複製代碼

執行結果:

4、Newtonsoft.Json(JSON.NET)將序列化結果保存到指定的文件:

複製代碼
            User movie = new User { Name = "張三", Age = 1993 };
            using (System.IO.StreamWriter file = System.IO.File.CreateText(@"F:\UserInfo.txt"))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(file, movie);
            }
複製代碼
    public class User
    {
        public string Name { set; get; }
        public int Age { set; get; }
    }

執行後保存到文件的結果:

5、Newtonsoft.Json(JSON.NET)基於枚舉類型的JsonConverters轉換器

複製代碼
            List<JosnEnum> list = new List<JosnEnum> { JosnEnum.NotStartus, JosnEnum.Startus };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(list);
            Console.WriteLine(json);
            Console.WriteLine("");

            System.Collections.Generic.Dictionary<string, int> dic = new System.Collections.Generic.Dictionary<string, int>
            { 
            {((JosnEnum)(int)JosnEnum.NotStartus).ToString() ,(int)JosnEnum.NotStartus} ,
            {((JosnEnum)(int)JosnEnum.Startus).ToString() ,(int)JosnEnum.Startus}
            };
            string dicJson = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
            Console.WriteLine(dicJson);

            Console.WriteLine("");
            List<JosnEnum> list2 = new List<JosnEnum>
            {
                JosnEnum.NotStartus, 
                JosnEnum.Startus
            };
            string json3 = Newtonsoft.Json.JsonConvert.SerializeObject(list2, new Newtonsoft.Json.Converters.StringEnumConverter());
            Console.WriteLine(json3);

            Console.WriteLine("");

            List<JosnEnum> result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JosnEnum>>(json3, new Newtonsoft.Json.Converters.StringEnumConverter());
            Console.WriteLine(string.Join(", ", result.Select(c => c.ToString())));
複製代碼
    public enum JosnEnum
    {
        NotStartus = 0,
        Startus = 1
    }

執行結果:

6、Newtonsoft.Json(JSON.NET)通過JRaw將JS函數序列化到JSON中

複製代碼
            JavaScriptSettings settings = new JavaScriptSettings
            {
                OnLoadFunction = new Newtonsoft.Json.Linq.JRaw("OnLoad"),
                OnSucceedFunction = new Newtonsoft.Json.Linq.JRaw("function(e) { alert(e); }")
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(settings, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);
複製代碼
    public class JavaScriptSettings
    {
        public Newtonsoft.Json.Linq.JRaw OnLoadFunction { get; set; }
        public Newtonsoft.Json.Linq.JRaw OnSucceedFunction { get; set; }
    }

7、使用Newtonsoft.Json(JSON.NET)將json反序列化對象

複製代碼
  string json = @"{
   'Email': '[email protected]',
   'Active': true,
   'CreatedDate': '2016-11-26 20:39',
   'Roles': [
     'User',
     'Admin'
]
 }";
            Account account = Newtonsoft.Json.JsonConvert.DeserializeObject<Account>(json);
            Console.WriteLine(account.Email);
複製代碼
複製代碼
    public class Account
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<string> Roles { get; set; }
    }
複製代碼

執行結果:

8、使用Newtonsoft.Json(JSON.NET)反序列化List集合:

            string json = @"['Html5','C#','.Net','.Net Core']";
            List<string> videogames = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(json);
            Console.WriteLine(string.Join(", ", videogames));

執行結果:

9、使用Newtonsoft.Json(JSON.NET)反序列化dictionary字典

            string json = @"{'Name': '張三','Age': '23'}";
            var htmlAttributes = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
            Console.WriteLine(htmlAttributes["Name"]);
            Console.WriteLine(htmlAttributes["Age"]);

執行結果:

10、使用Newtonsoft.Json(JSON.NET)序列化var匿名類型,有時候,我們並不需要先定義一個類,然後new一個對象後再進行序列化,JSON.NET支持匿名類型的序列化和反序列化。

複製代碼
            var test1 = new { Name = "李四", Age = 26 };
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(test1);
            Console.WriteLine(json);

            Console.WriteLine("");
            var test2 = new { Name = "", Age = 0 };
            string json1 = @"{'Name':'張三','Age':'25'}";
            var result = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(json1, test2);
            Console.WriteLine(result.Name);
複製代碼

執行結果:

11、Newtonsoft.Json(JSON.NET)用新JSON字符串填充指定對象的屬性值

複製代碼
            Account account = new Account
            {
                Email = "[email protected]",
                Active = true,
                CreatedDate = DateTime.Now,
                Roles = new List<string> { "User", "Admin" }
            };
            string json = @"{'Active': false, 'Roles': ['Expired']}";
            Newtonsoft.Json.JsonConvert.PopulateObject(json, account);
            Console.WriteLine(account.Active);
            Console.WriteLine(account.Email);
複製代碼
複製代碼
    public class Account
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<string> Roles { get; set; }
    }
複製代碼

執行結果:

12、使用Newtonsoft.Json(JSON.NET)反序列化時可指定構造函數:

首先我們定義如下的類型,我們希望JSON.NET反序列化對象時使用第2個構造函數,我們將第一個默認構造函數屏蔽,標記爲私有private修飾符。第2個構造函數需要指定一個website對象作爲參數,如果提供的參數爲null則拋出異常:

複製代碼
public class Website
{
    public string Url { get; set; }
    private Website()
    {
    }
    public Website(Website website)
    {
        if (website == null) throw new ArgumentNullException("website");
        Url = website.Url;
    }
}
複製代碼

現在使用一般的方式反序列化一個JSON字符串。執行出現的結果:

我們發現該序列化方法拋出了異常,並沒有按照我們預想的方式進行反序列化,JSON.NET提供如下的方式指定公有構造函數。

複製代碼
            string json = @"{'Url':'http://www.cnblogs.com/linJie1930906722/'}";
            Website website = Newtonsoft.Json.JsonConvert.DeserializeObject<Website>(json, new Newtonsoft.Json.JsonSerializerSettings
            {
                ConstructorHandling = Newtonsoft.Json.ConstructorHandling.AllowNonPublicDefaultConstructor
            });
            Console.WriteLine(website.Url);
複製代碼

執行結果:

另外,JSON.NET提供了指定任何構造函數的JsonConstructorAttribute特性,只需要在構造函數上標記,即可指定構造函數。

複製代碼
    public class Users
    {
        public string UserName { get; private set; }
        public bool Enabled { get; private set; }
        public Users()
        {
        }
        [Newtonsoft.Json.JsonConstructor]
        public Users(string userName, bool enabled)
        {
            UserName = userName;
            Enabled = enabled;
        }
    }
複製代碼
            string json = @"{""UserName"": ""希特勒"",""Enabled"": true}";
            Users user = Newtonsoft.Json.JsonConvert.DeserializeObject<Users>(json);
            Console.WriteLine(user.UserName);

執行結果:

13、當對象的屬性爲默認值(0或null)時不序列化該屬性

複製代碼
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Person Partner { get; set; }
        public decimal? Salary { get; set; }
    }
複製代碼
複製代碼
            Person person1 = new Person();
            string json1 = Newtonsoft.Json.JsonConvert.SerializeObject(person1, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
            {
                DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore
            });
            Console.WriteLine(json1);

            Console.WriteLine("");
            Person person2 = new Person(){Name = "奧巴馬"};
            string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(person2, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
            {
                DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore
            });
            Console.WriteLine(json2);
複製代碼

執行結果:

14、Newtonsoft.Json(JSON.NET)中忽略null值得處理器

複製代碼
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Person Partner { get; set; }
        public decimal? Salary { get; set; }
    }
複製代碼
複製代碼
            Person person = new Person { Name = "張三", Age = 46 };
            string jsonIncludeNullValues = Newtonsoft.Json.JsonConvert.SerializeObject(person, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(jsonIncludeNullValues);
            Console.WriteLine("");
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(person, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
            {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
            });
            Console.WriteLine(json);
複製代碼

執行結果:

15、JSON.NET中循環引用的處理方法

複製代碼
            Employee employee1 = new Employee { Name = "張三" };
            Employee employee2 = new Employee { Name = "李四" };
            employee1.Manager = employee2;
            employee2.Manager = employee2;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(employee1, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
            {
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            });
            Console.WriteLine(json);
複製代碼
    public class Employee
    {
        public string Name { get; set; }
        public Employee Manager { get; set; }
    }

執行結果:

16、通過ContractResolver指定屬性名首字母小寫,通常,在.NET中屬性採用PascalCase規則(首字母大寫),在JavaScript中屬性名使用CamelCase規則(首字母小寫),我們希望序列化後的JSON字符串符合CamelCase規則,JSON.NET提供的ContractResolver可以設置屬性名小寫序列化

    public class User
    {
        public string Name { set; get; }
        public int Age { set; get; }
    }
複製代碼
            User person = new User { Name = "張三", Age =52 };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(person, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            });
            Console.WriteLine(json);
複製代碼

執行結果:

17、JSON.NET中通過特性序列化枚舉類型

複製代碼
    public enum ProductStatus
    { 
        NotConfirmed, 
        Active, Deleted
    }

    public class Product
    {
        public string Name { get; set; }

        [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
        public ProductStatus Status { get; set; }
    }
複製代碼
            Product user = new Product { Name = @"羽絨服", Status = ProductStatus.Deleted };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(user, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);

執行結果:

18、指定需要序列化的屬性

複製代碼
    [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)]
    public class Categroy
    {
        //Id不需要序列化
        public Guid Id { get; set; }

        [Newtonsoft.Json.JsonProperty]
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty]
        public int Size { get; set; }
    }
複製代碼
複製代碼
            Categroy categroy = new Categroy
            {
                Id = Guid.NewGuid(),
                Name = "內衣",
                Size = 52
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(categroy, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);
複製代碼

執行結果:

19、序列化對象時指定屬性名

複製代碼
    public class Videogame
    {
        [Newtonsoft.Json.JsonProperty("name")]
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty("release_date")]
        public DateTime ReleaseDate { get; set; }
    }
複製代碼
複製代碼
            Videogame starcraft = new Videogame
            {
                Name = "英雄聯盟",
                ReleaseDate = DateTime.Now
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(starcraft, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);
複製代碼

執行結果:

20、序列化時指定屬性在JSON中的順序

複製代碼
    public class Personl
    {
        [Newtonsoft.Json.JsonProperty(Order = 2)]
        public string FirstName { get; set; }

        [Newtonsoft.Json.JsonProperty(Order = 1)]
        public string LastName { get; set; }
    }
複製代碼
            Personl person = new Personl { FirstName = "張三", LastName = "李四" };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(person, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);

執行結果:

21、反序列化指定屬性是否必須有值必須不爲null,在反序列化一個JSON時,可通過JsonProperty特性的Required指定反序列化行爲,當反序列化行爲與指定的行爲不匹配時,JSON.NET將拋出異常,Required是枚舉,Required.Always表示屬性必須有值切不能爲null,Required.AllowNull表示屬性必須有值,但允許爲null值。

複製代碼
    public class Order
    {
        [Newtonsoft.Json.JsonProperty(Required = Newtonsoft.Json.Required.Always)]
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty(Required = Newtonsoft.Json.Required.AllowNull)]
        public DateTime? ReleaseDate { get; set; }
    }
複製代碼
複製代碼
            string json = @"{
    'Name': '促銷訂單',
    'ReleaseDate': null
  }";
            Order order = Newtonsoft.Json.JsonConvert.DeserializeObject<Order>(json);
            Console.WriteLine(order.Name);
            Console.WriteLine(order.ReleaseDate);
複製代碼

執行結果:

22、通過特性指定null值忽略序列化

複製代碼
    public class Vessel
    {
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty(NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public DateTime? LaunchDate { get; set; }
    }
複製代碼
            Vessel vessel = new Vessel { Name = "張三" };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(vessel, Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);

執行結果:

23、忽略不需要序列化的屬性,並不是對象所有屬性都要參與序列化,我們可以使用JsonIgnore特性排除不需要序列化的屬性,下面示例中的PasswordHash將被忽略。

複製代碼
    public class Accounts
    {
        public string FullName { get; set; }
        public string EmailAddress { get; set; }
        [Newtonsoft.Json.JsonIgnore]
        public string PasswordHash { get; set; }
    }
複製代碼
複製代碼
            Accounts account = new Accounts
            {
                FullName = "admin",
                EmailAddress = "[email protected]",
                PasswordHash = "dfsfgerhtyhsasdhjyujtgwe454811sfsg8d"
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(account);
            Console.WriteLine(json);
複製代碼

執行結果:

24、序列化或反序列化時指定日期時間格式,JSON.NET中提供一個名爲JsonSerializerSettings的設置對象,可通過此對象設置很多序列化和反序列化的行爲,如果要設置JSON.NET序列化輸出的日期時間格式,只需要指定格式化字符串即可。通過JsonSerializerSettings的DateFormatString屬性指定日期時間格式:

複製代碼
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime CreateDate { get; set; }
    }
複製代碼
複製代碼
            Customer custom = new Customer { FirstName = "張三", LastName = "李四", CreateDate = DateTime.Now };
            Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings
            {
                DateFormatString = "yyyy-MM-dd HH:mm:ss",
                Formatting = Newtonsoft.Json.Formatting.Indented
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(custom, settings);
            Console.WriteLine(json);
複製代碼

執行結果:

 

發佈了21 篇原創文章 · 獲贊 61 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章