文章内容

2017/4/25 15:52:43,作 者: 黄兵

JSON使用JsonPropertyAttribute

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

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

  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 { getset; }  
  13.   
  14.         [JsonProperty("Chinese Director")]  
  15.         public string Director { getset; }  
  16.           
  17.         public int ReleaseYear { getset; }  
  18.     }  
  19. }  

实例:

using Newtonsoft.Json;
using System;


namespace PhotographyGallery.Models
{

public class BingImageResponse
{
[JsonProperty(PropertyName = "_type")]
public string _type { get; set; }
[JsonProperty(PropertyName = "value")]
public BingImage[] value { get; set; }
}

public class BingImage
{
[JsonProperty(PropertyName = "name")]
public string name { get; set; }
[JsonProperty(PropertyName = "webSearchUrl")]
public string webSearchUrl { get; set; }
[JsonProperty(PropertyName = "webSearchUrlPingSuffix")]
public string webSearchUrlPingSuffix { get; set; }
[JsonProperty(PropertyName = "thumbnailUrl")]
public string thumbnailUrl { get; set; }
[JsonProperty(PropertyName = "datePublished")]
public DateTime datePublished { get; set; }
[JsonProperty(PropertyName = "contentUrl")]
public string contentUrl { get; set; }
[JsonProperty(PropertyName = "hostPageUrl")]
public string hostPageUrl { get; set; }
[JsonProperty(PropertyName = "hostPageUrlPingSuffix")]
public string hostPageUrlPingSuffix { get; set; }
[JsonProperty(PropertyName = "contentSize")]
public string contentSize { get; set; }
[JsonProperty(PropertyName = "encodingFormat")]
public string encodingFormat { get; set; }
[JsonProperty(PropertyName = "hostPageDisplayUrl")]
public string hostPageDisplayUrl { get; set; }
[JsonProperty(PropertyName = "width")]
public int width { get; set; }
[JsonProperty(PropertyName = "height")]
public int height { get; set; }
[JsonProperty(PropertyName = "thumbnail")]
public Thumbnail thumbnail { get; set; }
[JsonProperty(PropertyName = "imageId")]
public string imageId { get; set; }
[JsonProperty(PropertyName = "accentColor")]
public string accentColor { get; set; }
}

public class Thumbnail
{
[JsonProperty(PropertyName = "width")]
public int width { get; set; }
[JsonProperty(PropertyName = "height")]
public int height { get; set; }
}

}

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

  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属性。

  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 { getset; }  
  13.   
  14.         [JsonProperty(Order=0)]  
  15.         public string Director { getset; }  
  16.           
  17.         public int ReleaseYear { getset; }  
  18.   
  19.         [JsonProperty(Order=-3)]  
  20.         public string ChiefActor { getset; }  
  21.   
  22.         [JsonProperty(Order=2)]  
  23.         public string ChiefActress { getset; }  
  24.     }  
  25. }  

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

  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可以为空.

  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 { getset; }  
  13.   
  14.         [JsonProperty(Required = Required.AllowNull)]  
  15.         public DateTime? ReleaseDate { getset; }  
  16.   
  17.         public string Director { getset; }  
  18.     }  
  19. }  

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

  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.

  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 { getset; }  
  12.   
  13.         [JsonProperty(IsReference=true)]  
  14.         public Director ExecuteDir { getset; }  
  15.     }  
  16. }  

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

  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 { getset; }  
  12.   
  13.         [JsonProperty(ItemIsReference=true)]  
  14.         public IList<Director> Directors { getset; }  
  15.     }  
  16. }  

3.序列化对象

  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

  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 { getset; }  
  12.   
  13.         public string Director { getset; }  
  14.   
  15.         [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]  
  16.         public DateTime? LaunchDate { getset; }  
  17.     }  
  18. }  

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

  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

分享到:

发表评论

评论列表