一、使用JsonIgnoreAttribute忽略其属性序列化
1.创建一个Movie对象,并在其属性上添加JsonIgnore.
- 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; }
- [JsonIgnore]
- 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 = "张建亚",
- LaunchDate=DateTime.Today
- };
- string json = JsonConvert.SerializeObject(m, Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
3.运行结果
二、使用ErrorHandlingAttribute忽略其属性异常
1.先创建一个Employee对象,必须得在属性Roles中get中添加抛出异常,然后用内部方法OnError,忽略其异常
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GongHuiNewtonsoft.Json.Serialization;
- using System.Runtime.Serialization;
- namespace JSONDemo
- {
- public class Employee
- {
- private List<string> _roles;
- public string Name { get; set; }
- public int Age { get; set; }
- public List<string> Roles
- {
- get
- {
- if (_roles == null)
- throw new Exception("Roles not loaded!");
- return _roles;
- }
- set { _roles = value; }
- }
- [OnError]
- internal void OnError(StreamingContext context, ErrorContext error)
- {
- error.Handled = true;
- }
- }
- }
2.调用代码,先实例化对象Employee,设置Roles属性为空,引发异常.这里,因为用了OnError方法,所以其异常忽略.
- 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)
- {
- Employee employee = new Employee
- {
- Name = "GongHui",
- Age = 28,
- Roles = null
- };
- string json = JsonConvert.SerializeObject(employee, Formatting.Indented);
- Console.WriteLine(json);
- }
- }
- }
3.运行的结果
三、使用DefaultValueAttribute设置属性默认值
1.先创建一个Address类,然后引用System.ComponentModel,设置属性的默认值.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ComponentModel;
- namespace JSONDemo
- {
- public class Address
- {
- public string Province { get; set; }
- public string City { get; set; }
- public string County { get; set; }
- [DefaultValue("GuangZhou")]
- public string DetailAddress
- {
- get{ return County + " " + City + " " + Province; }
- }
- }
- }
2.实例化Address对象,
- 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)
- {
- Address address = new Address();
- Console.WriteLine("----------------显示所有序列化成员---------------");
- string json = JsonConvert.SerializeObject(address, Formatting.Indented);
- Console.WriteLine(json);
- Console.WriteLine("----------------仅显示添加默认值的成员-------------");
- string json1 = JsonConvert.SerializeObject(address, Formatting.Indented, new JsonSerializerSettings
- {
- DefaultValueHandling = DefaultValueHandling.Ignore
- });
- Console.WriteLine(json1);
- }
- }
- }
3.运行的结果