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. hive集成sentry的sql使用语法

    Sentry权限控制通过Beeline(Hiveserver2 SQL 命令行接口)输入Grant 和 Revoke语句来配置.语法跟现在的一些主流的关系数据库很相似.需要注意的是:当sentry服务 ...

  2. 【Android Developers Training】 73. 布局变化的动画

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. [Paper Reading]--Exploiting Relevance Feedback in Knowledge Graph

    <Exploiting Relevance Feedback in Knowledge Graph> Publication: KDD 2015 Authors: Yu Su, Sheng ...

  4. 入门Android开发

    一个工作1年多一点的前端狗,由于公司需要,开始接触Android,也是第一次写博客,以后学到的技术每天都会写篇博客,让我们一起进步. Android 系统开发应用程序,为我们提供了哪些东西. 一.四大 ...

  5. VB6之扫雷克星

    很久之前,那时候我还不太会玩(现在也不厉害)扫雷这个游戏,同学总在我面前炫耀自己的技术有多叼.“高级,99颗雷,只需三分钟...”,如此这般.也许确实需要天赋,我总要排查个半天才敢点下左键,然后就BO ...

  6. FileOutputStreamTest

    package JBJADV003; import java.io.FileOutputStream;import java.io.OutputStream;import java.io.IOExce ...

  7. JavaScript第三课 (循环)

    循环语句       !如果至少需要执行一次循环体,就用do … while语句,一般情况下用while语句就可以了. while 语法:一直读取循环到条件为假时停止循环. while(条件) { 语 ...

  8. input file 上传图片问题

    html代码如下: <input id="fileup" type="file" accept="image/*" capture=& ...

  9. 第2篇:用as3.0制作一个滚动条组件

    本实例演示了实现一个滚动条基本功能的制作方法,没有添加改变皮肤,修改滚动条视框大小等功能,有兴趣的朋友可根据自己要求自行添加.使用时只需要通过以下一行代码创建滚动条组件: var myScrollba ...

  10. Unity3D文件读取

    Resources: 是作为一个Unity3D的保留文件夹出现的,也就是如果你新建的文件夹的名字叫Resources,那么里面的内容在打包时都会被无条件的打到发布包中.它的特点简单总结一下就是: 只读 ...