一、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. 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对象,然后序列化。

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

[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. 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对象,然后序列化。

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

[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. 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。

[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. 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.

[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. 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.

[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. 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.序列化对象

[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. 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

[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. 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对象,然后序列化

[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. 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

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

转载于:https://www.cnblogs.com/1175429393wljblog/p/8066061.html

Newtonsoft.Json 序列化和反序列化 以及时间格式 2相关推荐

  1. php json字符串序列化,JSON序列化与反序列化实现方法(附代码)

    这次给大家带来JSON序列化与反序列化实现方法(附代码),JSON序列化与反序列化实现的注意事项有哪些,下面就是实战案例,一起来看一下. 一.JSON简介 JSON(JavaScript Object ...

  2. MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式

    MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式 2015-01-18 00:11 https://www.muhanxue.com/essa ...

  3. .NET MVC第九章、Web Api Json序列化与反序列化

    .NET MVC第九章.Web Api Json序列化与反序列化 目录 .NET MVC第九章.Web Api Json序列化与反序列化 json数据格式 JSON 语法 返回对象 Json序列化 反 ...

  4. Newtonsoft.Json序列化库

    Unity自己的Json序列化是不支持字典格式的,而且功能比较单一,这里介绍一个.Net中开源的Json序列化和反序列化库和基本用法以及常用的数据处理方法(github地址:https://githu ...

  5. JSON序列化和反序列化

    1.什么是json序列化和反序列化? json序列化:就是JavaBean对象转化为JSON格式的字符串. 反序列化:就是序列化的反方向,将字符串转化为JavaBean. 2.为什么要序列化和反序列化 ...

  6. 一文读懂Json序列化与反序列化

    一文读懂Json序列化与反序列化 一文读懂Json序列化与反序列化 #mermaid-svg-tVjnnlFu6ZBDpGOQ {font-family:"trebuchet ms" ...

  7. LocalDateTime的JSON序列化和反序列化

    jackson的json中Date的序列化和反序列化 import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jac ...

  8. 【Unity百宝箱】游戏中的用户数据存档 | Json序列化和反序列化 | 数据加密和解密 | 干货游戏教程

    目录 框架设计 工具选用 逻辑书写 框架使用 框架优化 数据加密 总结 最后 Hi 大家好,我是游戏区Bug打工人小棋. 在游戏开发过程中,我们经常有存储用户数据的这一需求,比方说:游戏音量.关卡进度 ...

  9. DotNet的JSON序列化与反序列化

    JSON(JavaScript Object Notation)JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.在现在的通信中,较多的采用JSON数据格式,JSON有 ...

最新文章

  1. 对抗 Google优势 微软考虑收购雅虎
  2. Linux串口原理与编程
  3. mongooseim xmpp 服务器docker 安装试用
  4. Robocopy是微软Windows Server 2003资源工具包中众多多用途的实用程序之一(它是基于强大的拷贝程序...
  5. 这个「化学家」登上Nature封面:工作007,8天完成近700次实验,还设计出新催化剂...
  6. 行波和驻波动画演示gif_新技能get√ | 语文课上的笔顺动画可以这么做
  7. springboot发送http请求
  8. hibernate java内存一次能取多少条_Hibernate管理Session和批量操作分析
  9. 将一串随机数输入到二维坐标轴中,不断刷新JPanel,实现动态显示的效果微笑
  10. phpMyAdmin 配置
  11. matlab2c使用c++实现matlab函数系列教程-compan函数
  12. WebView的简单使用
  13. 【病毒程序】发一个无聊的小病毒(无限弹窗)
  14. 网站劫持原理及分析网站被劫持了有几种解决方法
  15. mysql assertion_Mysql异常崩溃,提示 Failing assertion: extern_len = part_len
  16. 相机标定实验过程注意问题及总结
  17. 第34次中国互联网络发展状况统计报告
  18. 2020年创业风口:社交电商
  19. GPU服务器的上手使用-小试牛刀
  20. EDUCoder编程练习题解(结构体)

热门文章

  1. 二元偏导数存在的条件_偏导数连续怎么证明
  2. python批量删除数据库记录_GitHub - TracyMcgrady6/pymsql_Operation: Python3操作mysql数据库,实现增、批量增、删、改、查...
  3. AtCoder Beginner Contest 171 B - Mix Juice
  4. 联想电脑计算机怎么设置十进制,如何在win10系统中设置电池充电阈值
  5. 2021中国大学生程序设计竞赛部分题解(CCPC)- 网络选拔赛(重赛)
  6. grpc python 负载均衡_Ambassador 0.52 新特性:会话亲和性、负载均衡控制、gRPC-Web
  7. 推荐模块︱apple.Turicreate个性化推荐recommender(五)
  8. mysql并发replace死锁
  9. 浅析Mysql的隔离级别及MVCC
  10. Go语言中的结构体 (struct)