Newtonsoft.Json 序列化和反序列化 以及时间格式 2
一、JSON使用JsonPropertyAttribute重命名属性名
1.先创建一个Movie对象,然后在其属性上添加JsonProperty,并指定重命名的名称。注意:属性Name和Director已指定。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GongHuiNewtonsoft.Json;
- namespace JSONDemo
- {
- public class Movie
- {
- [JsonProperty("name")]
- public string Name { get; set; }
- [JsonProperty("Chinese Director")]
- public string Director { get; set; }
- public int ReleaseYear { get; set; }
- }
- }
2.实例化Movie对象,然后序列化。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using GongHuiNewtonsoft.Json;
- using GongHuiNewtonsoft.Json.Serialization;
- using GongHuiNewtonsoft.Json.Converters;
- namespace JSONDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Movie m = new Movie
- {
- Name = "非诚勿扰1",
- Director = "冯小刚",
- ReleaseYear = 2008
- };
- string json = JsonConvert.SerializeObject(m, Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
3.运行结果,注意:属性ReleaseYear未被重命名。
二、JSON使用JsonPropertyAttribute序列化升序排序属性
1.先创建一个Movie对象,然后指定JsonProperty,并添加Order属性。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GongHuiNewtonsoft.Json;
- namespace JSONDemo
- {
- public class Movie
- {
- [JsonProperty(Order=4)]
- public string Name { get; set; }
- [JsonProperty(Order=0)]
- public string Director { get; set; }
- public int ReleaseYear { get; set; }
- [JsonProperty(Order=-3)]
- public string ChiefActor { get; set; }
- [JsonProperty(Order=2)]
- public string ChiefActress { get; set; }
- }
- }
2.实例化Movie对象,然后序列化。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using GongHuiNewtonsoft.Json;
- using GongHuiNewtonsoft.Json.Serialization;
- using GongHuiNewtonsoft.Json.Converters;
- namespace JSONDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Movie m = new Movie
- {
- Name = "非诚勿扰1",
- Director = "冯小刚",
- ReleaseYear = 2008,
- ChiefActor="葛优",
- ChiefActress="舒淇"
- };
- string json = JsonConvert.SerializeObject(m, Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
3.运行结果。注意:未指定Order序号的属性,界定于大于负数小于正数,并按默认顺序排序。
三、JSON使用JsonPropertyAttribute反序列化属性时,Required指定属性性质
1.创建一个Movie对象,给属性添加JsonProperty,并指定其Required的性质。属性Name必须有值,DateTime可以为空.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GongHuiNewtonsoft.Json;
- namespace JSONDemo
- {
- public class Movie
- {
- [JsonProperty(Required=Required.Always)]
- public string Name { get; set; }
- [JsonProperty(Required = Required.AllowNull)]
- public DateTime? ReleaseDate { get; set; }
- public string Director { get; set; }
- }
- }
2.实例化Movie对象,反序列化JSON。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using GongHuiNewtonsoft.Json;
- using GongHuiNewtonsoft.Json.Serialization;
- using GongHuiNewtonsoft.Json.Converters;
- namespace JSONDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- string json = @"{
- 'Name':'举起手来1',
- 'Director':'冯小宁',
- 'ReleaseDate':null
- }";
- Movie m = JsonConvert.DeserializeObject<Movie>(json);
- Console.WriteLine(m.Name);
- Console.WriteLine(m.Director);
- Console.WriteLine(m.ReleaseDate);
- }
- }
- }
3.运行结果是
四、JSON使用JsonPropertyAttribute序列化引用类型集合
1.创建一个Director对象,并声明一个本身类型的属性,指定JsonProperty中的IsReference为true.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GongHuiNewtonsoft.Json;
- namespace JSONDemo
- {
- public class Director
- {
- public string Name { get; set; }
- [JsonProperty(IsReference=true)]
- public Director ExecuteDir { get; set; }
- }
- }
2.创建一个Movie对象,声明一个Director集合的属性,指定JsonProperty中的IsReference为true.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GongHuiNewtonsoft.Json;
- namespace JSONDemo
- {
- public class Movie
- {
- public string Name { get; set; }
- [JsonProperty(ItemIsReference=true)]
- public IList<Director> Directors { get; set; }
- }
- }
3.序列化对象
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using GongHuiNewtonsoft.Json;
- using GongHuiNewtonsoft.Json.Serialization;
- using GongHuiNewtonsoft.Json.Converters;
- namespace JSONDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Director dir = new Director
- {
- Name = "冯小刚"
- };
- Director dir1 = new Director
- {
- Name = "张艺谋",
- ExecuteDir = dir
- };
- Movie m = new Movie
- {
- Name = "满城尽带黄金甲",
- Directors = new List<Director>
- {
- dir,
- dir1
- }
- };
- string json = JsonConvert.SerializeObject(m, Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
4.运行结果
五、JSON使用JsonPropertyAttribute序列化忽略属性null
1.创建一个Movie对象,并在属性上指定JsonProperty,添加NullValueHandling,忽略null
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GongHuiNewtonsoft.Json;
- namespace JSONDemo
- {
- public class Movie
- {
- public string Name { get; set; }
- public string Director { get; set; }
- [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
- public DateTime? LaunchDate { get; set; }
- }
- }
2.实例化对象Movie对象,然后序列化
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using GongHuiNewtonsoft.Json;
- using GongHuiNewtonsoft.Json.Serialization;
- using GongHuiNewtonsoft.Json.Converters;
- namespace JSONDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Movie m = new Movie
- {
- Name = "爱情呼叫转移",
- Director = "张建亚"
- };
- string json = JsonConvert.SerializeObject(m, Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
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相关推荐
- php json字符串序列化,JSON序列化与反序列化实现方法(附代码)
这次给大家带来JSON序列化与反序列化实现方法(附代码),JSON序列化与反序列化实现的注意事项有哪些,下面就是实战案例,一起来看一下. 一.JSON简介 JSON(JavaScript Object ...
- MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式
MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式 2015-01-18 00:11 https://www.muhanxue.com/essa ...
- .NET MVC第九章、Web Api Json序列化与反序列化
.NET MVC第九章.Web Api Json序列化与反序列化 目录 .NET MVC第九章.Web Api Json序列化与反序列化 json数据格式 JSON 语法 返回对象 Json序列化 反 ...
- Newtonsoft.Json序列化库
Unity自己的Json序列化是不支持字典格式的,而且功能比较单一,这里介绍一个.Net中开源的Json序列化和反序列化库和基本用法以及常用的数据处理方法(github地址:https://githu ...
- JSON序列化和反序列化
1.什么是json序列化和反序列化? json序列化:就是JavaBean对象转化为JSON格式的字符串. 反序列化:就是序列化的反方向,将字符串转化为JavaBean. 2.为什么要序列化和反序列化 ...
- 一文读懂Json序列化与反序列化
一文读懂Json序列化与反序列化 一文读懂Json序列化与反序列化 #mermaid-svg-tVjnnlFu6ZBDpGOQ {font-family:"trebuchet ms" ...
- LocalDateTime的JSON序列化和反序列化
jackson的json中Date的序列化和反序列化 import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jac ...
- 【Unity百宝箱】游戏中的用户数据存档 | Json序列化和反序列化 | 数据加密和解密 | 干货游戏教程
目录 框架设计 工具选用 逻辑书写 框架使用 框架优化 数据加密 总结 最后 Hi 大家好,我是游戏区Bug打工人小棋. 在游戏开发过程中,我们经常有存储用户数据的这一需求,比方说:游戏音量.关卡进度 ...
- DotNet的JSON序列化与反序列化
JSON(JavaScript Object Notation)JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.在现在的通信中,较多的采用JSON数据格式,JSON有 ...
最新文章
- 对抗 Google优势 微软考虑收购雅虎
- Linux串口原理与编程
- mongooseim xmpp 服务器docker 安装试用
- Robocopy是微软Windows Server 2003资源工具包中众多多用途的实用程序之一(它是基于强大的拷贝程序...
- 这个「化学家」登上Nature封面:工作007,8天完成近700次实验,还设计出新催化剂...
- 行波和驻波动画演示gif_新技能get√ | 语文课上的笔顺动画可以这么做
- springboot发送http请求
- hibernate java内存一次能取多少条_Hibernate管理Session和批量操作分析
- 将一串随机数输入到二维坐标轴中,不断刷新JPanel,实现动态显示的效果微笑
- phpMyAdmin 配置
- matlab2c使用c++实现matlab函数系列教程-compan函数
- WebView的简单使用
- 【病毒程序】发一个无聊的小病毒(无限弹窗)
- 网站劫持原理及分析网站被劫持了有几种解决方法
- mysql assertion_Mysql异常崩溃,提示 Failing assertion: extern_len = part_len
- 相机标定实验过程注意问题及总结
- 第34次中国互联网络发展状况统计报告
- 2020年创业风口:社交电商
- GPU服务器的上手使用-小试牛刀
- EDUCoder编程练习题解(结构体)
热门文章
- 二元偏导数存在的条件_偏导数连续怎么证明
- python批量删除数据库记录_GitHub - TracyMcgrady6/pymsql_Operation: Python3操作mysql数据库,实现增、批量增、删、改、查...
- AtCoder Beginner Contest 171 B - Mix Juice
- 联想电脑计算机怎么设置十进制,如何在win10系统中设置电池充电阈值
- 2021中国大学生程序设计竞赛部分题解(CCPC)- 网络选拔赛(重赛)
- grpc python 负载均衡_Ambassador 0.52 新特性:会话亲和性、负载均衡控制、gRPC-Web
- 推荐模块︱apple.Turicreate个性化推荐recommender(五)
- mysql并发replace死锁
- 浅析Mysql的隔离级别及MVCC
- Go语言中的结构体 (struct)