随笔 - 86  文章 - 0  评论 - 737  阅读 - 18万

『动态』动态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   InkFx  阅读(512)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示