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
Newtonsoft.Json 序列化和反序列化 以及时间格式 2的更多相关文章
- Newtonsoft.Json 序列化和反序列化 以及时间格式 2 高级使用
手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...
- Newtonsoft.Json 序列化和反序列化 以及时间格式
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- Newtonsoft.Json序列化字符串-格式化和时间格式问题
最近C#中需要将实体进行json序列化,使用了Newtonsoft.Json public static void TestJson() { DataTable d ...
- Newtonsoft.Json 序列化和反序列化 时间格式
From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...
- Newtonsoft.Json 序列化和反序列化 时间格式 [转]
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- [转]Newtonsoft.Json 序列化和反序列化 时间格式
本文转自:http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeObj ...
- Newtonsoft.Json 序列化和反序列化 时间格式【转】
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- C#中Newtonsoft.Json 序列化和反序列化 时间格式
步骤 引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; 格式配置 IsoDateTimeConverter timeFormat ...
- json的序列化和反序列化支持时间格式转换
.NET自带的json序列有时间格式问题,为了解决自己写了个json格式的序列化和反序列化 1.引入的命名空间 using System; using System.Collections.Gener ...
随机推荐
- J2EE开发实战基础系列一 HelloWorld
开始咱们的第一个程序,首先是配置环境,按照上一章所描述的方式下载开发工具,然后配置Java环境变量,给大家看下具体的结构: 环境变量配置OK的提示,如上图. Eclipse和Tomcat的文件目录位置 ...
- [Elasticsearch] 多字段搜索 (三) - multi_match查询和多数字段
multi_match查询 multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询. NOTE 存在几种类型的multi_match查询,其中的3种正好和在"了解你的数据 ...
- PHP判断类型的方法
1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...
- Maven中mirrors和repository的关系
一.前言 之前没有搞清楚pom.xml里面配置的repositorys节点配置的仓库和mirrors里面配置的仓库的“镜像”间的关系,特意去搜索了相关内容,这边有篇文章讲得透彻,所以这边转载一下~ 二 ...
- ActiveMQ入门代码
Hello world程序演示: 生产者: package com.mq.helloworld; import javax.jms.Connection; import javax.jms.Conne ...
- 深入研究java.lang.Runtime类(转)
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- 【bzoj1086】[SCOI2005]王室联邦 树分块
题目描述 将一棵n个点的树分为若干“块”,每个块满足:大小在B到3B之间,并且这个“块”添加某个点后连通.求方案. 输入 第一行包含两个数N,B(1<=N<=1000, 1 <= B ...
- [CQOI2012]局部极小值
题目链接 注意到\(4\times 7\)的矩阵的局部极小值最多只有8个,可以状压. 设\(f[i][sta]\)表示从小到大填数,当前填到\(i\),极小值的填充状态为\(sta\)的方案数. 考虑 ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 解决Vue方法中setTimeout改变变量的值无效
把data里的变量继承过来重新封装一下 let that = this; this.rightAnswer = false; setTimeout(function() { that.rightAns ...