您的位置:首页 > Web前端 > JavaScript

JSON使用MissingMemberHandling 反序列化对象中不匹配成员的异常

2015-12-11 09:39 567 查看
1.创建一个Account对象,并添加成员.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JSONDemo
{
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreateDate { get; set; }
}
}


2.反序列化Account对象,当其JSON字符串中与原对象成员不匹配时出现异常

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json;

namespace JSONDemo
{
class Program
{
static void Main(string[] args)
{
string json = @"{
'Email':'359194966@qq.com',
'Actived':true,
'CreateDate':'2015-12-11 9:24:33',
'Roles':[
'GongHui',
'Jack'
]
}";

try
{
JsonConvert.DeserializeObject<Account>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Error
});
}
catch (JsonSerializationException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}


3.运行的结果



JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: