https://github.com/ststeiger/DynamicJSONserializer/blob/master/DynamicJSONserializer/Program.cs

namespace DynamicJSONserializer

  {
   
   
  class MainClass
  {
   
   
  public class DynamicArguments
  {
  System.Collections.Generic.Dictionary<string, object> dict;
   
  public System.Collections.IEnumerable Keys
  {
  get{
  return dict.Keys;
  }
   
  }
   
   
  public DynamicArguments()
  {
  dict = new System.Collections.Generic.Dictionary<string, object>();
  }
   
  public DynamicArguments(string json)
  {
  dict = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, object>>(json);
  }
   
   
  public T GetValue<T>(string key)
  {
  bool bIsNullable = false;
  bool hasValue = dict.ContainsKey(key);
  System.Type t = typeof(T);
   
  if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(System.Nullable<>))
  {
  t = System.Nullable.GetUnderlyingType(t);
  bIsNullable = true;
  }
   
  if (hasValue)
  {
  string value = System.Convert.ToString( dict[key], System.Globalization.CultureInfo.InvariantCulture);
   
  if (t == typeof(string))
  return (T)(object) value;
   
  if (!string.IsNullOrEmpty(value))
  {
  if (t == typeof(System.Int32))
  return (T)(object) System.Int32.Parse(value);
   
  if (t == typeof(System.UInt32))
  return (T)(object) System.UInt32.Parse(value);
   
  if (t == typeof(System.Int64))
  return (T)(object) System.Int64.Parse(value);
   
  if (t == typeof(System.UInt64))
  return (T)(object) System.UInt64.Parse(value);
   
  if (t == typeof(double))
  return (T)(object) double.Parse(value);
   
  if (t == typeof(float))
  return (T)(object) float.Parse(value);
   
  if (t == typeof(System.Guid))
  return (T)(object)new System.Guid(value);
   
  if (t == typeof(bool))
  {
  bool bReturnValue = false;
   
  if (bool.TryParse(value, out bReturnValue))
  return (T)(object)bReturnValue;
   
  if (value == "0")
  return (T)(object)false;
   
  if(System.StringComparer.OrdinalIgnoreCase.Equals(value,"YES"))
  return (T)(object)true;
   
  if(System.StringComparer.OrdinalIgnoreCase.Equals(value,"NO"))
  return (T)(object)false;
   
  System.Int64 lng;
  if (System.Int64.TryParse(value, out lng))
  return (T) (object) true;
   
  double dbl;
  if (double.TryParse(value, out dbl))
  return (T)(object) true;
   
  return (T)(object)false;
  }
   
  if (t == typeof(System.DateTime))
  {
  if((value.IndexOf('T') != -1) || (value.IndexOf('/') != -1 ))
  return (T)(object) System.DateTime.Parse(value, System.Globalization.CultureInfo.InvariantCulture);
   
  if(value.IndexOf('.') != -1)
  return (T) (object) System.DateTime.ParseExact(value, "dd.MM.yyyy", new System.Globalization.CultureInfo("de-CH", false));
   
  return (T)(object) System.DateTime.Parse(value, System.Globalization.CultureInfo.InvariantCulture);
  } // End if (t == typeof(DateTime))
   
  if(t == typeof(System.Enum))
  return (T) (object) System.Enum.Parse(t, value);
   
  if (t == typeof(DynamicArguments))
  return (T)(object) (new DynamicArguments(value));
   
  } // End if (!string.IsNullOrEmpty(value))
   
  } // End if (hasValue)
   
  if (bIsNullable)
  return (T) (object) null;
   
  T val = default(T);
  return (T) (object) val;
  } // End Function GetValue<T>(key)
   
   
  } // End Class
   
   
  public static void Main (string[] args)
  {
  Newtonsoft.Json.JsonSerializerSettings jss = new Newtonsoft.Json.JsonSerializerSettings();
  jss.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
  string str = Newtonsoft.Json.JsonConvert.SerializeObject(new System.DateTime(2013, 12, 31), jss);
  System.Console.WriteLine(str);
   
   
   
  string json = @"{ ""testobj"": {""foo"": ""bar"", ""lol"": ""rofl""}, ""stichtag_msft"": ""\/Date(1388444400000+0100)\/"", ""stichtag_deCH"": ""15.03.2015"", ""stichtag_jsondate"": ""2015-06-22T18:02:00.725Z"", ""gb_uid"":""2ba62b36-8b30-457c-8946-82fa452c99fb"",""key2"":""value2"", ""key3"" : 123, ""TSK_DatumVon"" : ""2013-01-01"", ""key5"": true, ""so_uid"": null }";
   
   
   
   
  DynamicArguments da = new DynamicArguments(json);
   
  foreach(var k in da.Keys)
  {
  System.Console.WriteLine(k);
  }
   
  string testobj = da.GetValue<string>("testobj");
  System.Console.WriteLine(testobj);
   
   
  DynamicArguments da2 = da.GetValue<DynamicArguments>("testobj");
  string foo = da2.GetValue<string>("foo");
  System.Console.WriteLine(foo);
   
  System.DateTime dt = da.GetValue<System.DateTime>("stichtag");
  System.Console.WriteLine(dt);
   
   
  // Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  System.Collections.Generic.Dictionary<string, dynamic> values =
  Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, dynamic>>(json);
   
  foreach (System.Collections.Generic.KeyValuePair<string, dynamic> kvp in values)
  {
  System.Console.WriteLine(kvp.Key);
  System.Console.WriteLine(kvp.Value.GetType());
  }
   
  System.Console.WriteLine(" --- Press any key to continue --- ");
   
  }
  }
  }

DynamicJSONserializer的更多相关文章

随机推荐

  1. C#—泛型_推迟一切可以推迟的东西

    泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...

  2. [leetcode-521-Longest Uncommon Subsequence I]

    Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...

  3. java网络编程之socket

    网络编程是什么 网络编程的本质是两个设备之间的数据交换,当然,在计算机网络中,设备主要指计算机.数据传递本身没有多大的难度,不就是把一个设备中的数据发送给两外一个设备,然后接受另外一个设备反馈的数据. ...

  4. HTML DOM元素关系与操作

    <html> <head><title>DOM元素关系与操作</title></head> <body> <!-- div ...

  5. Centos操作系统在虚拟机VMware上的安装

    1.下载centos操作系统,提供百度云盘链接:http://pan.baidu.com/s/1pLHOR03 2.打开上篇在VMware中新建好的空白虚拟机,将centos安装在此空白虚拟机上,步骤 ...

  6. 第三章:3.3 post 请求

    1. 在 from表单中将 属性 methtod="post‘ 改变成post 2. 访问主页地址:http://localhost:8000/index 3. 以上出现的错误. 查资料发现 ...

  7. Sublime Text 关闭自动更新的办法

    解决关于Submit Text每次打开 都会跳出更新选项的问题 Windows 下: 打开 Preferences —— Settings—User { "color_scheme" ...

  8. JavaWeb 后端 <九> 之 JDBC加强

    一.大结果集的分页(重点,难点) 1.分批次查询:分页 2.基于数据库的分页:依赖的是数据库的分页语句(不同数据库是不同的) MySQL:每页显示10条. select * from XXX limi ...

  9. Html 学习

    行内元素和块级元素 行内元素(行级元素) 多个元素会在一行内显示 块级元素 独立成行 注意:块级元素能够嵌套行内元素 <div> <span></span> < ...

  10. 使用intelliJ创建 spring boot + gradle + mybatis站点

    Spring boot作为快速入门是不错的选择,现在似乎没有看到大家写过spring boot + gradle + mybatis在intellij下的入门文章,碰巧.Net同事问到,我想我也可以写 ...