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序列化的更多相关文章

  1. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  2. json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式

     JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...

  3. Newtonsoft.Json序列化日期时间去T的几种方式。

    原文地址:MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式. http://www.cnblogs.com/wuball/p/4231343. ...

  4. MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式。

    原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Re ...

  5. .Net深入实战系列—JSON序列化那点事儿

    序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...

  6. C#中JSON序列化和反序列化

    有一段时间没有到博客园写技术博客了,不过每天逛逛博客园中大牛的博客还是有的,学无止境…… 最近在写些调用他人接口的程序,用到了大量的JSON.XML序列化和反序列化,今天就来总结下json的序列化和反 ...

  7. 使用JSON.Net(Newtonsoft.Json)作为ASP.Net MVC的json序列化和反序列化工具

    ASP.Net MVC默认的JSON序列化使用的是微软自己的JavaScriptSerializer.性能低不说,最让人受不了的是Dictionary<,>和Hashtable类型居然对应 ...

  8. NetworkComms V3 使用Json序列化器进行网络通信

    刚才在网上闲逛,偶然看到一篇文章 C#(服务器)与Java(客户端)通过Socket传递对象 网址是:http://www.cnblogs.com/iyangyuan/archive/2012/12/ ...

  9. Newtonsoft.Json 序列化和反序列化 时间格式

    From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...

随机推荐

  1. elasticsearch插件三—— Marvel插件安装详解

    2016年05月21日 22:58:13 阅读数:23058 一.Marvel插件介绍 Marvel插件:在簇中从每个节点汇集数据.这个插件必须每个节点都得安装. Marvel是Elasticsear ...

  2. MongoDB中关于查询条件中包括集合中字段的查询

    要查询的数据结构例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvMzY5NTE=/font/5a6L5L2T/fontsize/400/f ...

  3. phpQuery对数据信息的采集进一步学习

    前提:需要下载:phpQuery/phpQuery.php 链接:http://www.cnblogs.com/wuheng1991/p/5145398.html 1.对于规则的部分 <?php ...

  4. 【python】pyqt练习

    import sys from PyQt4.QtCore import * from PyQt4.QtGui import * import ui_price class PriceDlg(QDial ...

  5. python3----练习题(过滑块验证)

    # 导入模块 from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webd ...

  6. phpstorm 内存设置

    https://blog.csdn.net/qq_33862644/article/details/81938970

  7. Using Notepad++ To Quickly Format XML

    http://geek.sylvainw.com/2010/03/28/using-notepad-to-quickly-format-xml/ My favorite way to quickly ...

  8. 应用IBatisNet+Castle进行项目的开发

    最近在做一个项目,项目的需求不够明确,这是做项目的大忌,但是没有办法.项目的架构采用Dotnet平台使用C#进行开发,为了加快项目的开发进度,采用代码生成工具之MyGeneration 生成业务基本代 ...

  9. mongodb在32位机的连接

    Windows 32bit版本安装Mongodb时,会发生的下面问题 2016-05-09T00:09:45.124+0800 I STORAGE  [initandlisten] exception ...

  10. Go开发的体会【转】

    摘自 http://studygolang.com/articles/5069 再次表示感谢,学习了.