Newtonsoft.Json 序列化和反序列化 以及时间格式 2

一、JSON使用JsonPropertyAttribute重命名属性名

1.先创建一个Movie对象,然后在其属性上添加JsonProperty,并指定重命名的名称。注意:属性Name和Director已指定。

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using GongHuiNewtonsoft.Json;  
  6.   
  7. namespace JSONDemo  
  8. {      
  9.     public class Movie  
  10.     {  
  11.         [JsonProperty("name")]  
  12.         public string Name { get; set; }  
  13.   
  14.         [JsonProperty("Chinese Director")]  
  15.         public string Director { get; set; }  
  16.           
  17.         public int ReleaseYear { get; set; }  
  18.     }  
  19. }  


2.实例化Movie对象,然后序列化。

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json;  
  7. using GongHuiNewtonsoft.Json.Serialization;  
  8. using GongHuiNewtonsoft.Json.Converters;  
  9.   
  10. namespace JSONDemo  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Movie m = new Movie  
  17.             {  
  18.                 Name = "非诚勿扰1",  
  19.                 Director = "冯小刚",  
  20.                 ReleaseYear = 2008  
  21.             };  
  22.   
  23.             string json = JsonConvert.SerializeObject(m, Formatting.Indented);  
  24.             Console.WriteLine(json);  
  25.         }  
  26.     }  
  27. }  


3.运行结果,注意:属性ReleaseYear未被重命名。

二、JSON使用JsonPropertyAttribute序列化升序排序属性

1.先创建一个Movie对象,然后指定JsonProperty,并添加Order属性。

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using GongHuiNewtonsoft.Json;  
  6.   
  7. namespace JSONDemo  
  8. {      
  9.     public class Movie  
  10.     {  
  11.         [JsonProperty(Order=4)]  
  12.         public string Name { get; set; }  
  13.   
  14.         [JsonProperty(Order=0)]  
  15.         public string Director { get; set; }  
  16.           
  17.         public int ReleaseYear { get; set; }  
  18.   
  19.         [JsonProperty(Order=-3)]  
  20.         public string ChiefActor { get; set; }  
  21.   
  22.         [JsonProperty(Order=2)]  
  23.         public string ChiefActress { get; set; }  
  24.     }  
  25. }  


2.实例化Movie对象,然后序列化。

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json;  
  7. using GongHuiNewtonsoft.Json.Serialization;  
  8. using GongHuiNewtonsoft.Json.Converters;  
  9.   
  10. namespace JSONDemo  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Movie m = new Movie  
  17.             {  
  18.                 Name = "非诚勿扰1",  
  19.                 Director = "冯小刚",  
  20.                 ReleaseYear = 2008,  
  21.                 ChiefActor="葛优",  
  22.                 ChiefActress="舒淇"  
  23.             };  
  24.   
  25.             string json = JsonConvert.SerializeObject(m, Formatting.Indented);  
  26.             Console.WriteLine(json);  
  27.         }  
  28.     }  
  29. }  


3.运行结果。注意:未指定Order序号的属性,界定于大于负数小于正数,并按默认顺序排序。

三、JSON使用JsonPropertyAttribute反序列化属性时,Required指定属性性质

1.创建一个Movie对象,给属性添加JsonProperty,并指定其Required的性质。属性Name必须有值,DateTime可以为空.

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using GongHuiNewtonsoft.Json;  
  6.   
  7. namespace JSONDemo  
  8. {      
  9.     public class Movie  
  10.     {  
  11.         [JsonProperty(Required=Required.Always)]  
  12.         public string Name { get; set; }  
  13.   
  14.         [JsonProperty(Required = Required.AllowNull)]  
  15.         public DateTime? ReleaseDate { get; set; }  
  16.   
  17.         public string Director { get; set; }  
  18.     }  
  19. }  


2.实例化Movie对象,反序列化JSON。

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json;  
  7. using GongHuiNewtonsoft.Json.Serialization;  
  8. using GongHuiNewtonsoft.Json.Converters;  
  9.   
  10. namespace JSONDemo  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             string json = @"{  
  17.                 'Name':'举起手来1',  
  18.                 'Director':'冯小宁',  
  19.                 'ReleaseDate':null  
  20.             }";  
  21.   
  22.             Movie m = JsonConvert.DeserializeObject<Movie>(json);  
  23.             Console.WriteLine(m.Name);  
  24.             Console.WriteLine(m.Director);  
  25.             Console.WriteLine(m.ReleaseDate);  
  26.         }  
  27.     }  
  28. }  


3.运行结果是

四、JSON使用JsonPropertyAttribute序列化引用类型集合

1.创建一个Director对象,并声明一个本身类型的属性,指定JsonProperty中的IsReference为true.

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using GongHuiNewtonsoft.Json;  
  6.   
  7. namespace JSONDemo  
  8. {  
  9.     public class Director  
  10.     {  
  11.         public string Name { get; set; }  
  12.   
  13.         [JsonProperty(IsReference=true)]  
  14.         public Director ExecuteDir { get; set; }  
  15.     }  
  16. }  

2.创建一个Movie对象,声明一个Director集合的属性,指定JsonProperty中的IsReference为true.

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using GongHuiNewtonsoft.Json;  
  6.   
  7. namespace JSONDemo  
  8. {      
  9.     public class Movie  
  10.     {  
  11.         public string Name { get; set; }  
  12.   
  13.         [JsonProperty(ItemIsReference=true)]  
  14.         public IList<Director> Directors { get; set; }  
  15.     }  
  16. }  


3.序列化对象

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json;  
  7. using GongHuiNewtonsoft.Json.Serialization;  
  8. using GongHuiNewtonsoft.Json.Converters;  
  9.   
  10. namespace JSONDemo  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Director dir = new Director  
  17.             {  
  18.                 Name = "冯小刚"  
  19.             };  
  20.   
  21.             Director dir1 = new Director  
  22.             {  
  23.                 Name = "张艺谋",  
  24.                 ExecuteDir = dir  
  25.             };  
  26.   
  27.             Movie m = new Movie  
  28.             {  
  29.                 Name = "满城尽带黄金甲",  
  30.                 Directors = new List<Director>  
  31.                 {  
  32.                     dir,  
  33.                     dir1  
  34.                 }  
  35.             };  
  36.   
  37.             string json = JsonConvert.SerializeObject(m, Formatting.Indented);  
  38.             Console.WriteLine(json);  
  39.         }  
  40.     }  
  41. }  


4.运行结果

五、JSON使用JsonPropertyAttribute序列化忽略属性null

1.创建一个Movie对象,并在属性上指定JsonProperty,添加NullValueHandling,忽略null

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using GongHuiNewtonsoft.Json;  
  6.   
  7. namespace JSONDemo  
  8. {      
  9.     public class Movie  
  10.     {  
  11.         public string Name { get; set; }  
  12.   
  13.         public string Director { get; set; }  
  14.   
  15.         [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]  
  16.         public DateTime? LaunchDate { get; set; }  
  17.     }  
  18. }  


2.实例化对象Movie对象,然后序列化

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json;  
  7. using GongHuiNewtonsoft.Json.Serialization;  
  8. using GongHuiNewtonsoft.Json.Converters;  
  9.   
  10. namespace JSONDemo  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Movie m = new Movie  
  17.             {  
  18.                 Name = "爱情呼叫转移",  
  19.                 Director = "张建亚"  
  20.             };  
  21.   
  22.             string json = JsonConvert.SerializeObject(m, Formatting.Indented);  
  23.             Console.WriteLine(json);  
  24.         }  
  25.     }  
  26. }  


3.运行的结果

JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751

原文链接:http://blog.csdn.net/lovegonghui/article/details/50272743

原文地址:https://www.cnblogs.com/1175429393wljblog/p/8066061.html