JSON使用JsonIgnoreAttribute,ErrorHandlingAttribute,DefaultValueAttribute
2015-12-12 12:02
746 查看
一、使用JsonIgnoreAttribute忽略其属性序列化
1.创建一个Movie对象,并在其属性上添加JsonIgnore.
2.实例化对象Movie,并给其忽略的属性赋值。
3.运行结果
二、使用ErrorHandlingAttribute忽略其属性异常
1.先创建一个Employee对象,必须得在属性Roles中get中添加抛出异常,然后用内部方法OnError,忽略其异常
2.调用代码,先实例化对象Employee,设置Roles属性为空,引发异常.这里,因为用了OnError方法,所以其异常忽略.
3.运行的结果
三、使用DefaultValueAttribute设置属性默认值
1.先创建一个Address类,然后引用System.ComponentModel,设置属性的默认值.
2.实例化Address对象,
3.运行的结果
JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751
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
相关文章推荐
- egret GUI 文本混排+文本链接的聊天解决方案【取巧法】
- UICollectionView 瀑布流 自定义cell 与表头表尾 自适应高度
- easy ui datagrid 设置冻结列
- 建造者(Builder)模式
- 根据索引获取Dictionary的key和value值
- UITextKit框架的学习(一)
- UICollectionView 瀑布流 初级
- uitableView 选择跳过后, 跳回 颜色变化 问题
- MIUI 判断悬浮窗权限
- 安装程序找不到office.zh-cn\OfficeMUI.msi。请浏览确定有效的安装源,然后单击”确定“。
- Java并发包中的同步队列SynchronousQueue实现原理
- BlockingQueue解析
- Hbuilder快捷入门
- UI中的继承源图
- hdu 3530 Subsequence 单调队列
- MUI之父页面刷新
- 关于uitableviewcell的accessoryType属性
- Legacy UEFI启动的比较
- BOIS模式 UEFI启动与Legacy启动鉴别
- 如何使用java自动生成数据表id,java.util.UUID类