惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Palo Alto Networks Blog
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
博客园_首页
博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
P
Privacy & Cybersecurity Law Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
Secure Thoughts
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Help Net Security
The Cloudflare Blog
Recorded Future
Recorded Future
Attack and Defense Labs
Attack and Defense Labs
J
Java Code Geeks
O
OpenAI News
T
Tor Project blog
B
Blog RSS Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
PCI Perspectives
PCI Perspectives
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
Cyberwarzone
Cyberwarzone
云风的 BLOG
云风的 BLOG
Security Latest
Security Latest
S
Schneier on Security
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
D
DataBreaches.Net
宝玉的分享
宝玉的分享
T
Troy Hunt's Blog
V
V2EX
Cisco Talos Blog
Cisco Talos Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Latest news
Latest news
量子位
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 西狐

我眼中的Java架构师 使用命令行写一个 Java Servlet 关于真正的Ajax方式上传文件 用jQuery轻松实现Div拖动 用CSS的 filter 来轻松实现图层半透明 用CSS3的 border-radius 来轻松实现图层圆角 用CSS3的 box-shadow 来轻松实现图层阴影效果 VS2010 让你的Javascript代码可以折叠 C# 将对象序列化为XML Lamborghini 兰博基尼 Gallardo Lamborghini 兰博基尼 Murcielago Lamborghini 兰博基尼 Reventon Lamborghini 兰博基尼 JavaScript 的 StringBuilder 无法加载"sybdrvado20.dll" 的原因和解决办法 js把 CheckBox 复选框 做成 radio 单选 的效果 搜狗"云"输入法,实现原理. - 西狐 - 博客园 TXT小说下载,交流 贾君鹏你妈妈喊你回家吃饭
JSON 转成 C# 动态类
西狐 · 2010-09-30 · via 博客园 - 西狐

任意 JSON 转成 C# 动态类,无需事先声明一个C#类型,实现json字符串转成dynamic 类

示例:

class Program
{
staticvoid Main(string[] args)
{
string json ="{name:'hooyes',pwd:'hooyespwd',books:{a:'红楼梦',b:'水浒传',c:{arr:['宝玉','林黛玉']}},arr:['good','very good']}";

dynamic dy

= ConvertJson(json);

Console.WriteLine(dy.name);

Console.WriteLine(dy.books.a);

Console.WriteLine(dy.arr[

1]);foreach (var s in dy.books.c.arr)
{
Console.WriteLine(s);
}

Console.Read();

}

static dynamic ConvertJson(string json)
{
JavaScriptSerializer jss
=new JavaScriptSerializer();
jss.RegisterConverters(
new JavaScriptConverter[] { new DynamicJsonConverter() });
dynamic dy
= jss.Deserialize(json, typeof(object)) as dynamic;
return dy;
}
}

用到两个自定义的类:DynamicJsonConverter,DynamicJsonObject

代码

publicclass DynamicJsonConverter : JavaScriptConverter
{
publicoverrideobject Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary ==null)
thrownew ArgumentNullException("dictionary");if (type ==typeof(object))
{
returnnew DynamicJsonObject(dictionary);
}
returnnull;
}
publicoverride IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
thrownew NotImplementedException();
}
publicoverride IEnumerable<Type> SupportedTypes
{
get { returnnew ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); }
}
}

代码

publicclass DynamicJsonObject : DynamicObject
{
private IDictionary<string, object> Dictionary { get; set; }public DynamicJsonObject(IDictionary<string, object> dictionary)
{
this.Dictionary = dictionary;
}
publicoverridebool TryGetMember(GetMemberBinder binder, outobject result)
{
result
=this.Dictionary[binder.Name];if (result is IDictionary<string, object>)
{
result
=new DynamicJsonObject(result as IDictionary<string, object>);
}
elseif (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
{
result
=new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x =>new DynamicJsonObject(x as IDictionary<string, object>)));
}
elseif (result is ArrayList)
{
result
=new List<object>((result as ArrayList).ToArray());
}
returnthis.Dictionary.ContainsKey(binder.Name);
}
}