Newtonsoft.Json 序列化和反序列化 以及时间格式 2
2017-12-19 15:41
483 查看
一、JSON使用JsonPropertyAttribute重命名属性名
1.先创建一个Movie对象,然后在其属性上添加JsonProperty,并指定重命名的名称。注意:属性Name和Director已指定。
[csharp] view plain copy
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对象,然后序列化。
[csharp] view plain copy
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属性。
[csharp] view plain copy
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对象,然后序列化。
[csharp] view plain copy
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可以为空.
[csharp] view plain copy
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。
[csharp] view plain copy
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.
[csharp] view plain copy
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.
[csharp] view plain copy
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.序列化对象
[csharp] view plain copy
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
[csharp] view plain copy
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对象,然后序列化
[csharp] view plain copy
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
1.先创建一个Movie对象,然后在其属性上添加JsonProperty,并指定重命名的名称。注意:属性Name和Director已指定。
[csharp] view plain copy
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对象,然后序列化。
[csharp] view plain copy
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属性。
[csharp] view plain copy
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对象,然后序列化。
[csharp] view plain copy
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可以为空.
[csharp] view plain copy
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。
[csharp] view plain copy
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.
[csharp] view plain copy
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.
[csharp] view plain copy
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.序列化对象
[csharp] view plain copy
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
[csharp] view plain copy
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对象,然后序列化
[csharp] view plain copy
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 序列化和反序列化 以及时间格式
- Newtonsoft.Json 序列化和反序列化 时间格式
- Newtonsoft.Json 序列化和反序列化 时间格式 [转]
- Newtonsoft.Json 序列化和反序列化 时间格式
- Newtonsoft.Json 序列化和反序列化 时间格式
- [转]Newtonsoft.Json 序列化和反序列化 时间格式
- Newtonsoft.Json同时对多个时间字段以不同的格式序列化
- Json.net动态序列化以及对时间格式的处理
- JSON 序列化和反序列化:将时间由"//Date(10000000000-0700)//" 格式转换成 "yyyy-MM-dd HH:mm:ss" 格式的字符串
- 转载自——Json.net动态序列化以及对时间格式的处理
- c# 使用 Newtonsoft.Json 序列化json字符串以及,反序列化对象
- Newtonsoft.Json序列化反序列化基础
- Java转换Json日期/Date(1487053489965+0800)/格式以及js时间格式 Tue Feb 14 2017 14:06:32 GMT+0800
- .NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
- Newtonsoft.Json序列化 对时间格式化处理
- C# .NET利用Newtonsoft.Json来序列化和反序列化对象
- 利用Newtonsoft.Json序列化,反序列化,读取,写入
- newTonSoft.Json进行序列化和反序列化
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程