Json.Net进行序列化反序列化(三)数据不匹配不报错?
默认设置的情况下,如果获取的数据串跟我们本地的字段数据不匹配,是不会报错的,这就会带来困扰,json.net其实可以通过配置来实现成员缺失时报错。
public class Account { public string FullName { get; set; } public bool Deleted { get; set; } }
string json = @"{ 'FullName': 'Dan Deleted', 'Deleted': true, 'DeletedDate': '2013-01-20T00:00:00' }"; try { JsonConvert.DeserializeObject<Account>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }); } catch (JsonSerializationException ex) { Console.WriteLine(ex.Message); // Could not find member 'DeletedDate' on object of type 'Account'. Path 'DeletedDate', line 4, position 23. }
- 下一篇:代码冰冻计划
- 上一篇:鸿蒙OS应用开发实践(一)