JSON作為一種輕量級的數(shù)據(jù)交換格式,簡單靈活,被很多系統(tǒng)用來數(shù)據(jù)交互,作為一名.NET開發(fā)人員,JSON.NET無疑是最好的序列化框架,支持XML和JSON序列化,高性能,免費(fèi)開源,支持LINQ查詢。目前已被微軟集成于webapi框架之中,因此,熟練掌握J(rèn)SON.NET相當(dāng)重要,這篇文章是零度參考官網(wǎng)整理的示例,通過這些示例,可以全面了解JSON.NET提供的功能。
Newtonsoft.Json的地址:
官網(wǎng):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 = "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; } }
執(zhí)行結(jié)果:
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);
執(zhí)行結(jié)果:
3、使用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);
執(zhí)行結(jié)果:
4、Newtonsoft.Json(JSON.NET)將序列化結(jié)果保存到指定的文件:
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; } }
執(zhí)行后保存到文件的結(jié)果:
5、Newtonsoft.Json(JSON.NET)基于枚舉類型的JsonConverters轉(zhuǎn)換器
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 }
執(zhí)行結(jié)果:
6、Newtonsoft.Json(JSON.NET)通過JRaw將JS函數(shù)序列化到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': '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; } }
執(zhí)行結(jié)果:
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));
執(zhí)行結(jié)果:
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"]);
執(zhí)行結(jié)果:
10、使用Newtonsoft.Json(JSON.NET)序列化var匿名類型,有時候,我們并不需要先定義一個類,然后new一個對象后再進(jìn)行序列化,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);
執(zhí)行結(jié)果:
11、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; } }
執(zhí)行結(jié)果:
12、使用Newtonsoft.Json(JSON.NET)反序列化時可指定構(gòu)造函數(shù):
首先我們定義如下的類型,我們希望JSON.NET反序列化對象時使用第2個構(gòu)造函數(shù),我們將第一個默認(rèn)構(gòu)造函數(shù)屏蔽,標(biāo)記為私有private修飾符。第2個構(gòu)造函數(shù)需要指定一個website對象作為參數(shù),如果提供的參數(shù)為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; }}
現(xiàn)在使用一般的方式反序列化一個JSON字符串。執(zhí)行出現(xiàn)的結(jié)果:
我們發(fā)現(xiàn)該序列化方法拋出了異常,并沒有按照我們預(yù)想的方式進(jìn)行反序列化,JSON.NET提供如下的方式指定公有構(gòu)造函數(shù)。
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);
執(zhí)行結(jié)果:
另外,JSON.NET提供了指定任何構(gòu)造函數(shù)的JsonConstructorAttribute特性,只需要在構(gòu)造函數(shù)上標(biāo)記,即可指定構(gòu)造函數(shù)。
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);
執(zhí)行結(jié)果:
13、當(dāng)對象的屬性為默認(rèn)值(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);
執(zhí)行結(jié)果:
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);
執(zhí)行結(jié)果:
15、JSON.NET中循環(huán)引用的處理方法
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; } }
執(zhí)行結(jié)果:
16、通過ContractResolver指定屬性名首字母小寫,通常,在.NET中屬性采用PascalCase規(guī)則(首字母大寫),在JavaScript中屬性名使用CamelCase規(guī)則(首字母小寫),我們希望序列化后的JSON字符串符合CamelCase規(guī)則,JSON.NET提供的ContractResolver可以設(shè)置屬性名小寫序列化
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);
執(zhí)行結(jié)果:
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);
執(zhí)行結(jié)果:
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 = "內(nèi)衣", Size = 52 }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(categroy, Newtonsoft.Json.Formatting.Indented); Console.WriteLine(json);
執(zhí)行結(jié)果:
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 = "英雄聯(lián)盟", ReleaseDate = DateTime.Now }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(starcraft, Newtonsoft.Json.Formatting.Indented); Console.WriteLine(json);
執(zhí)行結(jié)果:
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);
執(zhí)行結(jié)果:
21、反序列化指定屬性是否必須有值必須不為null,在反序列化一個JSON時,可通過JsonProperty特性的Required指定反序列化行為,當(dāng)反序列化行為與指定的行為不匹配時,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);
執(zhí)行結(jié)果:
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);
執(zhí)行結(jié)果:
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 = "1930906722@qq.com", PasswordHash = "dfsfgerhtyhsasdhjyujtgwe454811sfsg8d" }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(account); Console.WriteLine(json);
執(zhí)行結(jié)果:
24、序列化或反序列化時指定日期時間格式,JSON.NET中提供一個名為JsonSerializerSettings的設(shè)置對象,可通過此對象設(shè)置很多序列化和反序列化的行為,如果要設(shè)置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);
執(zhí)行結(jié)果: