一、JSON使用JsonPropertyAttribute重命名属性名
1.先创建一个Movie对象,然后在其属性上添加JsonProperty,并指定重命名的名称。注意:属性Name和Director已指定。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingGongHuiNewtonsoft.Json;
- namespaceJSONDemo
- {
- publicclassMovie
- {
- [JsonProperty("name")]
- publicstringName{get;set;}
- [JsonProperty("ChineseDirector")]
- publicstringDirector{get;set;}
- publicintReleaseYear{get;set;}
- }
- }
2.实例化Movie对象,然后序列化。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Data;
- usingGongHuiNewtonsoft.Json;
- usingGongHuiNewtonsoft.Json.Serialization;
- usingGongHuiNewtonsoft.Json.Converters;
- namespaceJSONDemo
- {
- classProgram
- {
- staticvoidMain(string[]args)
- {
- Moviem=newMovie
- {
- Name="非诚勿扰1",
- Director="冯小刚",
- ReleaseYear=2008
- };
- stringjson=JsonConvert.SerializeObject(m,Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
3.运行结果,注意:属性ReleaseYear未被重命名。
二、JSON使用JsonPropertyAttribute序列化升序排序属性
1.先创建一个Movie对象,然后指定JsonProperty,并添加Order属性。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingGongHuiNewtonsoft.Json;
- namespaceJSONDemo
- {
- publicclassMovie
- {
- [JsonProperty(Order=4)]
- publicstringName{get;set;}
- [JsonProperty(Order=0)]
- publicstringDirector{get;set;}
- publicintReleaseYear{get;set;}
- [JsonProperty(Order=-3)]
- publicstringChiefActor{get;set;}
- [JsonProperty(Order=2)]
- publicstringChiefActress{get;set;}
- }
- }
2.实例化Movie对象,然后序列化。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Data;
- usingGongHuiNewtonsoft.Json;
- usingGongHuiNewtonsoft.Json.Serialization;
- usingGongHuiNewtonsoft.Json.Converters;
- namespaceJSONDemo
- {
- classProgram
- {
- staticvoidMain(string[]args)
- {
- Moviem=newMovie
- {
- Name="非诚勿扰1",
- Director="冯小刚",
- ReleaseYear=2008,
- ChiefActor="葛优",
- ChiefActress="舒淇"
- };
- stringjson=JsonConvert.SerializeObject(m,Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
3.运行结果。注意:未指定Order序号的属性,界定于大于负数小于正数,并按默认顺序排序。
三、JSON使用JsonPropertyAttribute反序列化属性时,Required指定属性性质
1.创建一个Movie对象,给属性添加JsonProperty,并指定其Required的性质。属性Name必须有值,DateTime可以为空.
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingGongHuiNewtonsoft.Json;
- namespaceJSONDemo
- {
- publicclassMovie
- {
- [JsonProperty(Required=Required.Always)]
- publicstringName{get;set;}
- [JsonProperty(Required=Required.AllowNull)]
- publicDateTime?ReleaseDate{get;set;}
- publicstringDirector{get;set;}
- }
- }
2.实例化Movie对象,反序列化JSON。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Data;
- usingGongHuiNewtonsoft.Json;
- usingGongHuiNewtonsoft.Json.Serialization;
- usingGongHuiNewtonsoft.Json.Converters;
- namespaceJSONDemo
- {
- classProgram
- {
- staticvoidMain(string[]args)
- {
- stringjson=@"{
- ‘Name‘:‘举起手来1‘,
- ‘Director‘:‘冯小宁‘,
- ‘ReleaseDate‘:null
- }";
- Moviem=JsonConvert.DeserializeObject<Movie>(json);
- Console.WriteLine(m.Name);
- Console.WriteLine(m.Director);
- Console.WriteLine(m.ReleaseDate);
- }
- }
- }
3.运行结果是
四、JSON使用JsonPropertyAttribute序列化引用类型集合
1.创建一个Director对象,并声明一个本身类型的属性,指定JsonProperty中的IsReference为true.
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingGongHuiNewtonsoft.Json;
- namespaceJSONDemo
- {
- publicclassDirector
- {
- publicstringName{get;set;}
- [JsonProperty(IsReference=true)]
- publicDirectorExecuteDir{get;set;}
- }
- }
2.创建一个Movie对象,声明一个Director集合的属性,指定JsonProperty中的IsReference为true.
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingGongHuiNewtonsoft.Json;
- namespaceJSONDemo
- {
- publicclassMovie
- {
- publicstringName{get;set;}
- [JsonProperty(ItemIsReference=true)]
- publicIList<Director>Directors{get;set;}
- }
- }
3.序列化对象
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Data;
- usingGongHuiNewtonsoft.Json;
- usingGongHuiNewtonsoft.Json.Serialization;
- usingGongHuiNewtonsoft.Json.Converters;
- namespaceJSONDemo
- {
- classProgram
- {
- staticvoidMain(string[]args)
- {
- Directordir=newDirector
- {
- Name="冯小刚"
- };
- Directordir1=newDirector
- {
- Name="张艺谋",
- ExecuteDir=dir
- };
- Moviem=newMovie
- {
- Name="满城尽带黄金甲",
- Directors=newList<Director>
- {
- dir,
- dir1
- }
- };
- stringjson=JsonConvert.SerializeObject(m,Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
4.运行结果
五、JSON使用JsonPropertyAttribute序列化忽略属性null
1.创建一个Movie对象,并在属性上指定JsonProperty,添加NullValueHandling,忽略null
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingGongHuiNewtonsoft.Json;
- namespaceJSONDemo
- {
- publicclassMovie
- {
- publicstringName{get;set;}
- publicstringDirector{get;set;}
- [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
- publicDateTime?LaunchDate{get;set;}
- }
- }
2.实例化对象Movie对象,然后序列化
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Data;
- usingGongHuiNewtonsoft.Json;
- usingGongHuiNewtonsoft.Json.Serialization;
- usingGongHuiNewtonsoft.Json.Converters;
- namespaceJSONDemo
- {
- classProgram
- {
- staticvoidMain(string[]args)
- {
- Moviem=newMovie
- {
- Name="爱情呼叫转移",
- Director="张建亚"
- };
- stringjson=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
原文地址:http://www.cnblogs.com/1175429393wljblog/p/8066061.html