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. [Unity] A* pathfinding project integrated with influence map

    简介 最近一阶段重温了一些关于游戏人工智能方面的书籍. 加强了对influence map的认知.想要亲自动手实现一下. 正如文章标题所示,这篇文章讲的是:如何将influence map的机制融入到 ...

  2. 【LeetCode】2. Two Sum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  3. java--while、do while、for三种循环体

    1.for可以记录执行次数: 2.while.do while的i放在sum的后面和for得到的执行次数和结果是一致的. 1.从执行结果来看,放在前面,虽然执行次数和i放在sum的后面是相同,但是结果 ...

  4. javaWeb第一天

    //第一个JavaWeb项目package com.chy.action; import java.io.IOException; import javax.servlet.ServletExcept ...

  5. CentOS下源码安装vsftpd-3.0.0,并设置指定用户访问指定目录(附带完整配置文件)

    1.卸载系统已经存在的ftp服务器 因为是源码安装,所以不能通过rpm -qa的方式查看是否已经安装ftp服务器,可以通过find / | grep vsftp*方式查看系统中存在哪些与vsftpd相 ...

  6. spring +springmvc+mybatis组合mybatis-config.xml文件配置

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC &q ...

  7. PKM你的知识需要管理

    有一段时间没有更新技术博客了~,大脑中总感觉有点东西要写,却不知道从哪里开始写~至少写点东西,也算是一个阶段的成长 学习(充电过程).工作(知识变现过程)不是简单重复,永远都是最值得去反思.玩味的事儿 ...

  8. 【ALB学习笔记】基于事件触发方式的串行通信接口数据接收案例

    基于事件触发方式的串行通信接口数据接收案例 广东职业技术学院  欧浩源 一.案例背景 之前写过一篇<基于多线程方式的串行通信接口数据接收案例>的博文,讨论了采用轮询方式接收串口数据的情况. ...

  9. UGUI射线检测

    1.Graphic Raycaster 主要用于UI上的射线检测,挂有这个组件的物体,必须要挂上Canvas这个组件(当挂上Graphic Raycaster时Canvas也会自动挂上). Ignor ...

  10. 使用URLConnection调用axis1.4开发的webservice

    写在前面: 调用webservice的方式有很多:1.直接在客户端使用工具生成客户端代码,将代码拷进项目中调用即可:2.使用对应的webservice框架来进行调用,比如如果我们我的服务端开发用的是a ...