- >(strJson); foreach (Customer c in _list) { Console.WriteLine(c.CustomerName); } /* 宋江 吴用 石秀 */ 3)反序列化字典 json.net 也能把 json 对象反序列化为一个泛型字典。json 对象的属性名和值会被添 加到一个字典中。 string strJson = @"{""ID"": 1,""CustomerName"": ""宋江"",""Memo"": ""天魁星""}"; Dictionary
- >(@"[ ""2009-09-09T00:00:00Z"", ""不能转换为日期"", [ 1 ], ""1977-02-20T00:00:00Z"", null, ""2000-12-01T00:00:00Z"" ]", new JsonSerializerSettings { Error =delegate(object obj,Newtonsoft.Json.Serialization.ErrorEventArgs args) { 赠人玫瑰,手留余香。 12 csAxp errors.Add(args.ErrorContext.Error.Message); args.ErrorContext.Handled = true; }, Converters ={new IsoDateTimeConverter()} }); foreach (DateTime dd in c) { Console.WriteLine(dd.ToString()); } 泛型列表 c 是一个日期类型的集合,其中有 3 个值不能成功序列化。如果不添加错 误事件处理,则序列化 出现错误。但通过错误事件处理,捕捉了其中的异常并使序列化继续下去。 输出结果: /* 2009-9-9 0:00:00 1977-2-20 0:00:00 2000-12-1 0:00:00 */ new JsonSerializerSettings { Error =delegate(object obj,Newtonsoft.Json.Serialization.ErrorEventArgs args) { errors.Add(args.ErrorContext.Error.Message); args.ErrorContext.Handled = true; }, Converters ={new IsoDateTimeConverter()} }); 在系列化设置中,添加错误事件处理: ·Error 属性是一个泛型委托: EventHandler