JSON使用JsonIgnoreAttribute,ErrorHandlingAttribute,DefaultValueAttribute
来源:互联网 发布:手机优先网络列表设置 编辑:程序博客网 时间:2024/05/29 11:04
一、使用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.运行的结果
JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751
0 0
- JSON使用JsonIgnoreAttribute,ErrorHandlingAttribute,DefaultValueAttribute
- JsonIgnoreAttribute,ErrorHandlingAttribute,DefaultValueAttribute
- C#中DefaultValueAttribute的使用
- 获取DefaultValueAttribute值
- 令人疑惑的defaultValueAttribute
- 【JSON】JSON 使用
- 使用JSON
- 使用json
- json使用
- Json使用
- json使用
- json使用
- JSON 使用
- json使用
- Json使用
- json 使用
- json使用
- json使用
- 【8】Dom4j和XPath
- 正则表达式
- Compare Strings
- Swift中的if语句使用where
- (转帖)SQL Server处理四亿三千万记录大型项目实录
- JSON使用JsonIgnoreAttribute,ErrorHandlingAttribute,DefaultValueAttribute
- USACO--Friday the Thirteenth
- 使用PohtoView出现java.lang.IllegalArgumentException: pointerIndex out of range
- Android Studio 快捷键
- sublime There are no packages available for installation错误
- 客户端提交内容的HTML内容过滤根据
- CPP 6th——类(7)
- View Frame、bounds、clipsToBounds、masksToBounds
- 我的2015---找寻真实的自己