一、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

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

Newtonsoft.Json 序列化和反序列化 以及时间格式 2的更多相关文章

  1. Newtonsoft.Json 序列化和反序列化 以及时间格式 2 高级使用

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

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

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  3. Newtonsoft.Json序列化字符串-格式化和时间格式问题

    最近C#中需要将实体进行json序列化,使用了Newtonsoft.Json public static void TestJson()        {            DataTable d ...

  4. Newtonsoft.Json 序列化和反序列化 时间格式

    From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...

  5. Newtonsoft.Json 序列化和反序列化 时间格式 [转]

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  6. [转]Newtonsoft.Json 序列化和反序列化 时间格式

    本文转自:http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeObj ...

  7. Newtonsoft.Json 序列化和反序列化 时间格式【转】

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  8. C#中Newtonsoft.Json 序列化和反序列化 时间格式

    步骤 引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; 格式配置 IsoDateTimeConverter timeFormat ...

  9. json的序列化和反序列化支持时间格式转换

    .NET自带的json序列有时间格式问题,为了解决自己写了个json格式的序列化和反序列化 1.引入的命名空间 using System; using System.Collections.Gener ...

随机推荐

  1. mysql分布式技术

    所有的分布式技术 dobble zokkiper ngix

  2. Linux SPI总线和设备驱动架构之一:系统概述

    SPI是"Serial Peripheral Interface" 的缩写,是一种四线制的同步串行通信接口,用来连接微控制器.传感器.存储设备,SPI设备分为主设备和从设备两种,用 ...

  3. Mac上基于hexo+GitHub搭建个人博客(一)

    原文地址: http://fanjiajia.cn/2018/11/23/Mac%E4%B8%8A%E5%9F%BA%E4%BA%8Ehexo+GitHub%E6%90%AD%E5%BB%BA%E4% ...

  4. 关于<!DOCTYPE html>的学习(转)

    DOCTYPE是对Document type的缩写,说明用XHTML或者HTML是什么版本的.必须出现在<html>标签的前面,不需要关闭标签. <!DOCTYPE>声明不是标 ...

  5. ArcGIS10.2中文版安装和破解教程

    http://jingyan.baidu.com/article/e73e26c0cb5c1324adb6a791.html

  6. poi excel导出 xssf 带下拉框

    需求:导出之后带有二级级联的下拉框.(类似于省市). 最初的思路是怀疑是不是数组内串太多了,导出之后的excel有36行,调试的误区在于刚开始认为对行数有限制,后自己写了一个测试类,才发现不是行数,而 ...

  7. 一道ioccc题目

    国际C语言混乱大赛的一个题: main() {printf(&unix["\021%six\012\0"],(unix)["have"]+"f ...

  8. 【bzoj2768/bzoj1934】[JLOI2010]冠军调查/[Shoi2007]Vote 善意的投票 最小割

    bzoj2768 题目描述 一年一度的欧洲足球冠军联赛已经进入了淘汰赛阶段.随着卫冕冠军巴萨罗那的淘汰,英超劲旅切尔西成为了头号热门.新浪体育最近在吉林教育学院进行了一次大规模的调查,调查的内容就是关 ...

  9. 【bzoj1412】[ZJOI2009]狼和羊的故事 网络流最小割

    题目描述 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! Orez的羊狼圈 ...

  10. MySQL事物机制具备四点:简称ACID操作

    MySQL事物机制具备四点:简称ACID操作 1.原子性:要么都做,要么都不做(两条数据(写入和存储)一步未成功,整体回滚) 2.一致性:数据库的状态改变(两条数据(写入和存储)均成功,符合原子性,但 ...