JSON做爲一種輕量級的數據交換格式,簡單靈活,被不少系統用來數據交互,做爲一名.NET開發人員,JSON.NET無疑是最好的序列化框架,支持XML和JSON序列化,高性能,免費開源,支持LINQ查詢。目前已被微軟集成於webapi框架之中,所以,熟練掌握JSON.NET至關重要,這篇文章是零度參考官網整理的示例,經過這些示例,能夠全面瞭解JSON.NET提供的功能。html
Newtonsoft.Json的地址:git
官網:http://json.codeplex.com/github
源碼地址:https://github.com/JamesNK/Newtonsoft.Jsonweb
Newtonsoft.Json.dll下載:https://github.com/JamesNK/Newtonsoft.Json/releasesjson
一、使用Newtonsoft.Json(JSON.NET)序列化對象,經過Newtonsoft.Json.Formatting將json格式化輸出。c#
Account account = new Account { Email = "1930906722@qq.com", 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; } }
執行結果:api
二、使用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);
執行結果:ide
三、使用Newtonsoft.Json(JSON.NET)序列化dictionary字典函數
System.Collections.Generic.Dictionary<string, string> dic = new System.Collections.Generic.Dictionary<string, string> { { "Name", "張三" }, { "Age", "20" }, { "Email", "193090622@qq.com" } }; 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);
執行結果:
四、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; } }
執行後保存到文件的結果:
五、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 }
執行結果:
六、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; } }
七、使用Newtonsoft.Json(JSON.NET)將json反序列化對象
string json = @"{ 'Email': '1930906722@qq.com', '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; } }
執行結果:
八、使用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));
執行結果:
九、使用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"]);
執行結果:
十、使用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);
執行結果:
十一、Newtonsoft.Json(JSON.NET)用新JSON字符串填充指定對象的屬性值
Account account = new Account { Email = "1930906722@qq.com", 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; } }
執行結果:
十二、使用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);
執行結果:
1三、當對象的屬性爲默認值(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);
執行結果:
1四、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);
執行結果:
1五、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; } }
執行結果:
1六、經過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);
執行結果:
1七、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);
執行結果:
1八、指定須要序列化的屬性
[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);
執行結果:
1九、序列化對象時指定屬性名
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);
執行結果:
2一、反序列化指定屬性是否必須有值必須不爲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);
執行結果:
2二、經過特性指定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);
執行結果:
2三、忽略不須要序列化的屬性,並非對象全部屬性都要參與序列化,咱們能夠使用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 = "1930906722@qq.com", PasswordHash = "dfsfgerhtyhsasdhjyujtgwe454811sfsg8d" }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(account); Console.WriteLine(json);
執行結果:
2四、序列化或反序列化時指定日期時間格式,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);
執行結果: