分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

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

发布时间:2023-09-06 01:31责任编辑:赖小花关键词:暂无标签
一、JSON使用JsonPropertyAttribute重命名属性名

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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingGongHuiNewtonsoft.Json;
  6. namespaceJSONDemo
  7. {
  8. publicclassMovie
  9. {
  10. [JsonProperty("name")]
  11. publicstringName{get;set;}
  12. [JsonProperty("ChineseDirector")]
  13. publicstringDirector{get;set;}
  14. publicintReleaseYear{get;set;}
  15. }
  16. }


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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Data;
  6. usingGongHuiNewtonsoft.Json;
  7. usingGongHuiNewtonsoft.Json.Serialization;
  8. usingGongHuiNewtonsoft.Json.Converters;
  9. namespaceJSONDemo
  10. {
  11. classProgram
  12. {
  13. staticvoidMain(string[]args)
  14. {
  15. Moviem=newMovie
  16. {
  17. Name="非诚勿扰1",
  18. Director="冯小刚",
  19. ReleaseYear=2008
  20. };
  21. stringjson=JsonConvert.SerializeObject(m,Formatting.Indented);
  22. Console.WriteLine(json);
  23. }
  24. }
  25. }


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

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

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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingGongHuiNewtonsoft.Json;
  6. namespaceJSONDemo
  7. {
  8. publicclassMovie
  9. {
  10. [JsonProperty(Order=4)]
  11. publicstringName{get;set;}
  12. [JsonProperty(Order=0)]
  13. publicstringDirector{get;set;}
  14. publicintReleaseYear{get;set;}
  15. [JsonProperty(Order=-3)]
  16. publicstringChiefActor{get;set;}
  17. [JsonProperty(Order=2)]
  18. publicstringChiefActress{get;set;}
  19. }
  20. }


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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Data;
  6. usingGongHuiNewtonsoft.Json;
  7. usingGongHuiNewtonsoft.Json.Serialization;
  8. usingGongHuiNewtonsoft.Json.Converters;
  9. namespaceJSONDemo
  10. {
  11. classProgram
  12. {
  13. staticvoidMain(string[]args)
  14. {
  15. Moviem=newMovie
  16. {
  17. Name="非诚勿扰1",
  18. Director="冯小刚",
  19. ReleaseYear=2008,
  20. ChiefActor="葛优",
  21. ChiefActress="舒淇"
  22. };
  23. stringjson=JsonConvert.SerializeObject(m,Formatting.Indented);
  24. Console.WriteLine(json);
  25. }
  26. }
  27. }


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

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

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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingGongHuiNewtonsoft.Json;
  6. namespaceJSONDemo
  7. {
  8. publicclassMovie
  9. {
  10. [JsonProperty(Required=Required.Always)]
  11. publicstringName{get;set;}
  12. [JsonProperty(Required=Required.AllowNull)]
  13. publicDateTime?ReleaseDate{get;set;}
  14. publicstringDirector{get;set;}
  15. }
  16. }


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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Data;
  6. usingGongHuiNewtonsoft.Json;
  7. usingGongHuiNewtonsoft.Json.Serialization;
  8. usingGongHuiNewtonsoft.Json.Converters;
  9. namespaceJSONDemo
  10. {
  11. classProgram
  12. {
  13. staticvoidMain(string[]args)
  14. {
  15. stringjson=@"{
  16. ‘Name‘:‘举起手来1‘,
  17. ‘Director‘:‘冯小宁‘,
  18. ‘ReleaseDate‘:null
  19. }";
  20. Moviem=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.

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingGongHuiNewtonsoft.Json;
  6. namespaceJSONDemo
  7. {
  8. publicclassDirector
  9. {
  10. publicstringName{get;set;}
  11. [JsonProperty(IsReference=true)]
  12. publicDirectorExecuteDir{get;set;}
  13. }
  14. }

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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingGongHuiNewtonsoft.Json;
  6. namespaceJSONDemo
  7. {
  8. publicclassMovie
  9. {
  10. publicstringName{get;set;}
  11. [JsonProperty(ItemIsReference=true)]
  12. publicIList<Director>Directors{get;set;}
  13. }
  14. }


3.序列化对象

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Data;
  6. usingGongHuiNewtonsoft.Json;
  7. usingGongHuiNewtonsoft.Json.Serialization;
  8. usingGongHuiNewtonsoft.Json.Converters;
  9. namespaceJSONDemo
  10. {
  11. classProgram
  12. {
  13. staticvoidMain(string[]args)
  14. {
  15. Directordir=newDirector
  16. {
  17. Name="冯小刚"
  18. };
  19. Directordir1=newDirector
  20. {
  21. Name="张艺谋",
  22. ExecuteDir=dir
  23. };
  24. Moviem=newMovie
  25. {
  26. Name="满城尽带黄金甲",
  27. Directors=newList<Director>
  28. {
  29. dir,
  30. dir1
  31. }
  32. };
  33. stringjson=JsonConvert.SerializeObject(m,Formatting.Indented);
  34. Console.WriteLine(json);
  35. }
  36. }
  37. }


4.运行结果

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

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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingGongHuiNewtonsoft.Json;
  6. namespaceJSONDemo
  7. {
  8. publicclassMovie
  9. {
  10. publicstringName{get;set;}
  11. publicstringDirector{get;set;}
  12. [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
  13. publicDateTime?LaunchDate{get;set;}
  14. }
  15. }


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

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Data;
  6. usingGongHuiNewtonsoft.Json;
  7. usingGongHuiNewtonsoft.Json.Serialization;
  8. usingGongHuiNewtonsoft.Json.Converters;
  9. namespaceJSONDemo
  10. {
  11. classProgram
  12. {
  13. staticvoidMain(string[]args)
  14. {
  15. Moviem=newMovie
  16. {
  17. Name="爱情呼叫转移",
  18. Director="张建亚"
  19. };
  20. stringjson=JsonConvert.SerializeObject(m,Formatting.Indented);
  21. Console.WriteLine(json);
  22. }
  23. }
  24. }


3.运行的结果

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

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

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

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved