首页 > Web开发 > 详细

JSON使用JsonPropertyAttribute

时间:2020-05-16 09:41:44      阅读:54      评论:0      收藏:0      [点我收藏+]
原文: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. namespace JSONDemo
  7. {
  8. public class Movie
  9. {
  10. [JsonProperty("name")]
  11. public string Name { get; set; }
  12. [JsonProperty("Chinese Director")]
  13. public string Director { get; set; }
  14. public int ReleaseYear { get; set; }
  15. }
  16. }


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. namespace JSONDemo
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Movie m = new Movie
  16. {
  17. Name = "非诚勿扰1",
  18. Director = "冯小刚",
  19. ReleaseYear = 2008
  20. };
  21. string json = JsonConvert.SerializeObject(m, Formatting.Indented);
  22. Console.WriteLine(json);
  23. }
  24. }
  25. }


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. namespace JSONDemo
  7. {
  8. public class Movie
  9. {
  10. [JsonProperty(Order=4)]
  11. public string Name { get; set; }
  12. [JsonProperty(Order=0)]
  13. public string Director { get; set; }
  14. public int ReleaseYear { get; set; }
  15. [JsonProperty(Order=-3)]
  16. public string ChiefActor { get; set; }
  17. [JsonProperty(Order=2)]
  18. public string ChiefActress { get; set; }
  19. }
  20. }


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. namespace JSONDemo
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Movie m = new Movie
  16. {
  17. Name = "非诚勿扰1",
  18. Director = "冯小刚",
  19. ReleaseYear = 2008,
  20. ChiefActor="葛优",
  21. ChiefActress="舒淇"
  22. };
  23. string json = JsonConvert.SerializeObject(m, Formatting.Indented);
  24. Console.WriteLine(json);
  25. }
  26. }
  27. }


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. namespace JSONDemo
  7. {
  8. public class Movie
  9. {
  10. [JsonProperty(Required=Required.Always)]
  11. public string Name { get; set; }
  12. [JsonProperty(Required = Required.AllowNull)]
  13. public DateTime? ReleaseDate { get; set; }
  14. public string Director { get; set; }
  15. }
  16. }


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. namespace JSONDemo
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. string json = @"{
  16. ‘Name‘:‘举起手来1‘,
  17. ‘Director‘:‘冯小宁‘,
  18. ‘ReleaseDate‘:null
  19. }";
  20. Movie m = JsonConvert.DeserializeObject<Movie>(json);
  21. Console.WriteLine(m.Name);
  22. Console.WriteLine(m.Director);
  23. Console.WriteLine(m.ReleaseDate);
  24. }
  25. }
  26. }


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. namespace JSONDemo
  7. {
  8. public class Director
  9. {
  10. public string Name { get; set; }
  11. [JsonProperty(IsReference=true)]
  12. public Director ExecuteDir { get; set; }
  13. }
  14. }

 

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. namespace JSONDemo
  7. {
  8. public class Movie
  9. {
  10. public string Name { get; set; }
  11. [JsonProperty(ItemIsReference=true)]
  12. public IList<Director> Directors { get; set; }
  13. }
  14. }


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. namespace JSONDemo
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Director dir = new Director
  16. {
  17. Name = "冯小刚"
  18. };
  19. Director dir1 = new Director
  20. {
  21. Name = "张艺谋",
  22. ExecuteDir = dir
  23. };
  24. Movie m = new Movie
  25. {
  26. Name = "满城尽带黄金甲",
  27. Directors = new List<Director>
  28. {
  29. dir,
  30. dir1
  31. }
  32. };
  33. string json = JsonConvert.SerializeObject(m, Formatting.Indented);
  34. Console.WriteLine(json);
  35. }
  36. }
  37. }


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. namespace JSONDemo
  7. {
  8. public class Movie
  9. {
  10. public string Name { get; set; }
  11. public string Director { get; set; }
  12. [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
  13. public DateTime? LaunchDate { get; set; }
  14. }
  15. }


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. namespace JSONDemo
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Movie m = new Movie
  16. {
  17. Name = "爱情呼叫转移",
  18. Director = "张建亚"
  19. };
  20. string json = JsonConvert.SerializeObject(m, Formatting.Indented);
  21. Console.WriteLine(json);
  22. }
  23. }
  24. }


3.运行的结果

技术分享图片

 

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

 

JSON使用JsonPropertyAttribute

原文:https://www.cnblogs.com/lonelyxmas/p/12898234.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!