随笔-81  评论-709  文章-1 

『动态』动态JSON万能转换函数 + .Net40 dynamic动态数据绑定

不废话,调用代码:

 1         static void Main(string[] args)
 2         {
 3             string json = File.ReadAllText("2.txt", Encoding.Default);
 4 
 5             object result = JsonConvert.DeserializeObject(json);
 6             //dynamic result = JsonConvert.DeserializeObject<dynamic>(json);  //这行函数 啥用都没有 //虽然 result 是 dynamic 定义, 但其实还是 JObject 类型 —— 这种类型是 Newtonsoft.Json 中的一个实体
 7 
 8             //需要对 动态JSON 进行一个 转换
 9             dynamic result2 = JsonObjectToDynamic(result);
10             dynamic text = result2.DKS.Text;
11             Console.WriteLine(text);
12 
13 
14             //--------------------------------
15             //动态对象 测试
16             dynamic dyna = new DynamicModel();
17             dyna["Name"] = "张三";
18             dyna["Birthday"] = new DateTime(1990, 02, 15);
19 
20             Console.WriteLine(dyna.Name);
21             Console.WriteLine(dyna.Birthday);
22 
23 
24         }

 

运行截图:

 

完整Demo代码:

  1     class Program
  2     {
  3         static void Main(string[] args)
  4         {
  5             string json = File.ReadAllText("2.txt", Encoding.Default);
  6 
  7             object result = JsonConvert.DeserializeObject(json);
  8             //dynamic result = JsonConvert.DeserializeObject<dynamic>(json);  //这行函数 啥用都没有 //虽然 result 是 dynamic 定义, 但其实还是 JObject 类型 —— 这种类型是 Newtonsoft.Json 中的一个实体
  9 
 10             //需要对 动态JSON 进行一个 转换
 11             dynamic result2 = JsonObjectToDynamic(result);
 12             dynamic text = result2.DKS.Text;
 13             Console.WriteLine(text);
 14 
 15 
 16             //--------------------------------
 17             //动态对象 测试
 18             dynamic dyna = new DynamicModel();
 19             dyna["Name"] = "张三";
 20             dyna["Birthday"] = new DateTime(1990, 02, 15);
 21 
 22             Console.WriteLine(dyna.Name);
 23             Console.WriteLine(dyna.Birthday);
 24 
 25 
 26         }
 27 
 28         private static dynamic JsonObjectToDynamic(object jToken)
 29         {
 30             if (jToken == null) return null;
 31 
 32             if (jToken is JValue)
 33             {
 34                 return ((JValue) jToken).Value;
 35             }
 36             else if (jToken is JArray)
 37             {
 38                 dynamic list = new List<object>();
 39                 JArray array = (JArray)jToken; //JArray
 40                 if (array.HasValues)
 41                 {
 42                     foreach (object item in array) //JToken
 43                     {
 44                         object value = JsonObjectToDynamic(item);
 45                         list.Add(value);
 46                     }
 47                 }
 48                 return list;
 49             }
 50             //else if (string.Equals(typeName, "JContainer", StringComparison.InvariantCultureIgnoreCase))  //JObject JArray JConstructor 都继承自 JContainer 
 51             else if (jToken is JContainer)
 52             {
 53                 DynamicModel hash = new DynamicModel();
 54                 JContainer container = (JContainer)jToken;  //JContainer
 55                 if (container.HasValues)
 56                 {
 57                     foreach (JToken item in container) //JToken
 58                     {
 59                         if (item is JProperty)
 60                         {
 61                             JProperty itemProperty = (JProperty) item;
 62                             if (item.Type == JTokenType.Property)
 63                                 hash[itemProperty.Name] = JsonObjectToDynamic(itemProperty.Value);
 64                         }
 65                         else
 66                             throw new Exception(string.Format("JsonObjectToDynamic(*) 函数缺陷, 未知类型: " + item.GetType()));
 67                     }
 68                 }
 69                 return hash;
 70             }
 71 
 72             throw new Exception(string.Format("JsonObjectToDynamic(*) 函数缺陷, 未知类型: " + jToken.GetType()));
 73         }
 74         public class DynamicModel : DynamicObject
 75         {
 76             private Dictionary<string, object> m_Hash = new Dictionary<string, object>();
 77             //private Dictionary<string, object> m_Hash = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);  //不区分大小写
 78 
 79             public object this[string key]
 80             {
 81                 get
 82                 {
 83                     object value = null;
 84                     m_Hash.TryGetValue(key, out value);
 85                     return value;
 86                 }
 87                 set {
 88                     if (m_Hash.ContainsKey(key)) m_Hash[key] = value;
 89                     else m_Hash.Add(key, value);
 90                 }
 91             }
 92 
 93 
 94             public override bool TryGetMember(GetMemberBinder binder, out object result)
 95             {
 96                 string name = binder.Name;
 97                 return m_Hash.TryGetValue(name, out result);
 98             }
 99             public override bool TrySetMember(SetMemberBinder binder, object value)
100             {
101                 this[binder.Name] = value;
102                 return true;
103             }
104         }
105 
106 
107     }
View Code

 

posted on 2019-02-18 10:22 InkFx 阅读(...) 评论(...) 编辑 收藏