dymaic方式的Json序列化
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:

You can use it like this:
string json = ...;
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
dynamic obj = serializer.Deserialize(json, typeof(object));
So, given a JSON string:
{ "Items":[
{ "Name":"Apple", "Price":12.3 },
{ "Name":"Grape", "Price":3.21 } ],
"Date":"21/11/2010" }
The following code will work at runtime:
dynamic data = serializer.Deserialize(json, typeof(object));
data.Date; // "21/11/2010"
data.Items.Count; // 2
data.Items[0].Name; // "Apple"
data.Items[0].Price; // 12.3 (as a decimal)
data.Items[1].Name; // "Grape"
data.Items[1].Price; // 3.21 (as a decimal)
dymaic方式的Json序列化的更多相关文章
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式
JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...
- Newtonsoft.Json序列化日期时间去T的几种方式。
原文地址:MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式. http://www.cnblogs.com/wuball/p/4231343. ...
- MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式。
原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Re ...
- .Net深入实战系列—JSON序列化那点事儿
序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...
- C#中JSON序列化和反序列化
有一段时间没有到博客园写技术博客了,不过每天逛逛博客园中大牛的博客还是有的,学无止境…… 最近在写些调用他人接口的程序,用到了大量的JSON.XML序列化和反序列化,今天就来总结下json的序列化和反 ...
- 使用JSON.Net(Newtonsoft.Json)作为ASP.Net MVC的json序列化和反序列化工具
ASP.Net MVC默认的JSON序列化使用的是微软自己的JavaScriptSerializer.性能低不说,最让人受不了的是Dictionary<,>和Hashtable类型居然对应 ...
- NetworkComms V3 使用Json序列化器进行网络通信
刚才在网上闲逛,偶然看到一篇文章 C#(服务器)与Java(客户端)通过Socket传递对象 网址是:http://www.cnblogs.com/iyangyuan/archive/2012/12/ ...
- Newtonsoft.Json 序列化和反序列化 时间格式
From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...
随机推荐
- push images to private repostory
1.从官网pull 所需要的基础镜像 docker pull microsoft/mssql-server-windows-express 2.打上私有仓库标签 docker tag microsof ...
- 总结几个关于 jQuery 用法
有关 jquery 用法 目录: $.trim() $.inArray() $.getJSON() 事件委托 on 遍历closest() ajaxSubmit() 拖拽排序 dragsort() 进 ...
- beautifulSoup安装
Python2.7 + beautifulSoup 4.4.1 安装配置 原创 2016年05月09日 10:20:30 标签: python 1261 1. 前言 最近研究python 的爬虫功能, ...
- Matrix_tree Theorem 矩阵树定理学习笔记
Matrix_tree Theorem: 给定一个无向图, 定义矩阵A A[i][j] = - (<i, j>之间的边数) A[i][i] = 点i的度数 其生成树的个数等于 A的任意n ...
- SQL Server跨server之间訪问
在两个server都须要启用Ad Hoc Distributed Queries: EXEC sp_configure 'show advanced options', 1 RECONFIGURE E ...
- 使用Navicat for MySQL
1.打开Navicat for MySQL 2.新建连接 3.新建数据库 4.在新建的数据库上运行SQL文件
- 下载VMware
1.进入VMware官网:http://www.vmware.com/cn 2.找到下载,点击Workstation Pro,此时需要账号登录. 3.选择需要下载的版本.对应的操作系统,点击转至下载
- 一次Tomcat6.0.33版本号与6.0.44版本号差异所引发的问题
前序(公司应用为Web应用, 部署serverLinux + Nginx + Tomcat ) 一天收到公司报警邮件,显示个别机器方法调用严重超时,寻常都是在100ms以内响应的方法,突然某段时间响应 ...
- Spinner --- 功能和用法
第一种方法: 使用Spinner时需要配置选项的资源文件,资源文件为一个string类型的数组 在res下的values文件夹下新建一个xml文件 内容为: <?xml version=&quo ...
- 【BZOJ1067】[SCOI2007]降雨量 RMQ+特判
[BZOJ1067][SCOI2007]降雨量 Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年 ...