json 解析 dynamic 实例

  1. 添加system.web.extensions引用
  2. protected void Page_Load(object sender, EventArgs e)
  3.     {
  4.        string json = "{name:'hooyes',pwd:'hooyespwd',books:{a:'红楼梦',b:'水浒传',c:{arr:['宝玉','林黛玉']}},arr:['good','very good']}";  
  5.        System.Web.Script.Serialization.JavaScriptSerializer jss = new JavaScriptSerializer();
  6.        jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });  
  7.     
  8.        dynamic dy = jss.Deserialize(json, typeof(object)) as dynamic;
  9.        var name=dy.name;
  10.         
  11.     }
  12.     public class DynamicJsonConverter : JavaScriptConverter
  13.     {
  14.         public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
  15.         {
  16.             if (dictionary == null)
  17.                 throw new ArgumentNullException("dictionary");
  18.             if (type == typeof(object))
  19.             {
  20.                 return new DynamicJsonObject(dictionary);
  21.             }
  22.             return null;
  23.         }
  24.         public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
  25.         {
  26.             throw new NotImplementedException();
  27.         }
  28.         public override IEnumerable<Type> SupportedTypes
  29.         {
  30.             get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); }
  31.         }
  32.     }
  33.     public class DynamicJsonObject : DynamicObject
  34.     {
  35.         private IDictionary<string, object> Dictionary { get; set; }
  36.         public DynamicJsonObject(IDictionary<string, object> dictionary)
  37.         {
  38.             this.Dictionary = dictionary;
  39.         }
  40.         public override bool TryGetMember(GetMemberBinder binder, out object result)
  41.         {
  42.             result = this.Dictionary[binder.Name];
  43.             if (result is IDictionary<string, object>)
  44.             {
  45.                 result = new DynamicJsonObject(result as IDictionary<string, object>);
  46.             }
  47.             else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
  48.             {
  49.                 result = new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary<string, object>)));
  50.             }
  51.             else if (result is ArrayList)
  52.             {
  53.                 result = new List<object>((result as ArrayList).ToArray());
  54.             }
  55.             return this.Dictionary.ContainsKey(binder.Name);
  56.         }
  57.     }  

 

 

 

  1. public class DynamicJsonConverter : JavaScriptConverter  
  2.     {  
  3.         public override object Deserialize(IDictionary<stringobject> dictionary, Type type, JavaScriptSerializer serializer)  
  4.         {  
  5.             if (dictionary == null)  
  6.                 throw new ArgumentNullException("dictionary");  
  7.   
  8.             if (type == typeof(object))  
  9.             {  
  10.                 return new DynamicJsonObject(dictionary);  
  11.             }  
  12.   
  13.             return null;  
  14.         }  
  15.   
  16.         public override IDictionary<stringobject> Serialize(object obj, JavaScriptSerializer serializer)  
  17.         {  
  18.             throw new NotImplementedException();  
  19.         }  
  20.   
  21.         public override IEnumerable<Type> SupportedTypes  
  22.         {  
  23.             get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); }  
  24.         }  
  25.     }  

转载于:https://www.cnblogs.com/lyjpy456/p/4998378.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值