JSON使用ConstructorHanding反序列化非公共构造函数
来源:互联网 发布:网络教育作业答案 编辑:程序博客网 时间:2024/06/07 15:53
1.首先创建一个类,含有私有的构造函数.
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace JSONDemo{ public class Website { public string Url { get; set; } private Website() { } public Website(Website website) { if (website == null) throw new ArgumentNullException("website"); this.Url = website.Url; } }}
2.给类中的属性赋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 = @"{'Url':'http://blog.csdn.net/lovegonghui/article/details/50251873'}"; Website web = null; try { web = JsonConvert.DeserializeObject<Website>(json); } catch (Exception ex) { Console.WriteLine(ex.Message); //Console.WriteLine(web.Url);//未经处理的异常: System.NullReferenceException: 未将对象引用设置到对象的实例。 } Website website = JsonConvert.DeserializeObject<Website>(json, new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }); Console.WriteLine(website.Url); } }}
3.运行的结果是
JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751
0 0
- JSON使用ConstructorHanding反序列化非公共构造函数
- Yii使用公共函数
- Newtonsoft.Json 使用 C#序列号json,反序列化对象
- mvc使用Newtonsoft.Json反序列化json数据
- Newtonsoft.Json 使用 C#序列号json,反序列化对象
- Json.NET使用入门(四)【复杂Json反序列化】
- 使用Newtonsoft.Json进行JSON反序列化操作
- .net的json序列和反序列函数
- 【构造函数】----构造函数调用构造函数,使用this
- 使用构造函数
- 使用默认构造函数
- 使用构造函数
- 构造函数使用心得
- 构造函数的使用
- 构造函数使用
- 构造函数的使用
- 构造函数的使用
- 构造函数的使用
- 第十五周项目1-验证算法(1)
- 第十四周项目3是否二叉排序树?
- 第十二周项目4-利用遍历思想求解图问题(3)
- 第14周项目3 是否二叉排序树?
- 第14周项目1-(3)验证二叉排序树相关算法
- JSON使用ConstructorHanding反序列化非公共构造函数
- B-树的基本操作
- 第十三周项目1 prim算法
- 数据库的设计及登录锁定功能的思路
- 用哈希法组织关键字
- 第十五周项目1-验证算法(2)
- CList的扩展CGridListEx,可排序的grid表格控件
- 第14周项目2-二叉树排序树中查找的路径
- LeetCode 220 Contains Duplicate III