首页 / JSON / dymaic方式的Json序列化
dymaic方式的Json序列化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了dymaic方式的Json序列化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8082字,纯文字阅读大概需要12分钟。
内容图文
from:http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object
If you are happy to have a dependency upon the System.Web.Helpers
assembly, then you can use the Json class:
dynamic data =Json.Decode(json);
It is included with the MVC framework as an additional download to the .NET 4 framework. Be sure to give Vlad an upvote if that‘s helpful! However if you cannot assume the client environment includes this DLL, then read on.
An alternative deserialisation approach is suggested here. I modified the code slightly to fix a bug and suit my coding style. All you need is this code and a reference to System.Web.Extensions
from your project:
View Code
You can use it like this:
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">string json =...;
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">var serializer =newJavaScriptSerializer();
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">serializer.RegisterConverters(new[]{newDynamicJsonConverter()});
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">dynamic obj = serializer.Deserialize(json,typeof(object));
So, given a JSON string:
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">{"Items":[
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">{"Name":"Apple","Price":12.3},
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">{"Name":"Grape","Price":3.21}],
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">"Date":"21/11/2010"}
The following code will work at runtime:
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">dynamic data = serializer.Deserialize(json,typeof(object));
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">data.Date;// "21/11/2010"
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">data.Items.Count;// 2
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">data.Items[0].Name;// "Apple"
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">data.Items[0].Price;// 12.3 (as a decimal)
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">data.Items[1].Name;// "Grape"
<pre prettyprint="" prettyprinted"="" style="margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, ‘Lucida Console‘, ‘Liberation Mono‘, ‘DejaVu Sans Mono‘, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; line-height: 17.8048000335693px; background: #eeeeee;">data.Items[1].Price;// 3.21 (as a decimal)
原文:http://www.cnblogs.com/94cool/p/4225409.html
内容总结
以上是互联网集市为您收集整理的dymaic方式的Json序列化全部内容,希望文章能够帮你解决dymaic方式的Json序列化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。