• C# dynamic json


    对应普通对象,写个扩展方法,ToJson蛮方便。

    但是 dynamic 类型就不行了,因为是运行时解析,只能转换为强类型 IDictionary<string, object> 才可以。

    配置文件(Firebird):

     1 <configuration>
     2   <startup>
     3     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
     4   </startup>
     5 
     6   <connectionStrings>
     7     <add name="fb" connectionString="Server=localhost;User=SYSDBA;Password=1234;Database=wms;Dialect=3;ServerType=0;"/>
     8   </connectionStrings>
     9   
    10   <system.data>
    11     <DbProviderFactories>
    12       <remove invariant="FirebirdSql.Data.FirebirdClient" />
    13       <add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient"
    14            description=".NET Framework Data Provider for Firebird"
    15            type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
    16     </DbProviderFactories>
    17   </system.data>
    18 </configuration>

    转换工具:

     1     static class JsonHelper
     2     {
     3         public static string ToJson(this object obj)
     4         {
     5             var str = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings
     6             {
     7                 NullValueHandling = NullValueHandling.Ignore,
     8                 DateFormatString = "yyyy-MM-dd HH:mm:ss",
     9                 ReferenceLoopHandling = ReferenceLoopHandling.Ignore//循环引用
    10             });
    11             return str;
    12         }
    13 
    14         public static T FromJson<T>(this string json) where T : class
    15         {
    16             return JsonConvert.DeserializeObject(json, typeof(T), new IsoDateTimeConverter
    17             {
    18                 DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
    19             }) as T;
    20         }
    21     }

    Demo:

     1             var connectionString = ConfigurationManager.ConnectionStrings["fb"].ConnectionString;
     2             using (var db = new DbContext().ConnectionString(
     3                 connectionString, 
     4                 new DB2Provider(), 
     5                 FirebirdSql.Data.FirebirdClient.FirebirdClientFactory.Instance))
     6             {
     7                 List<dynamic> users = db.Sql(@"SELECT 
     8   M_USER.ID,
     9   M_USER.CODE,
    10   M_USER.PWD,
    11   M_USER.LABEL,
    12   M_USER.CRE_TIME
    13 FROM
    14   M_USER
    15 where id > @0").Parameters(20).QueryMany<dynamic>();
    16 
    17                 foreach (var user in users)
    18                 {
    19                     var tem = user as IDictionary<string, object>;
    20                     Console.WriteLine(tem.ToJson());
    21                 }
    22             }
    23 
    24             Console.ReadLine();

    效果:

  • 相关阅读:
    8.16
    8.6 总结
    Educational Codeforces Round 45 (Rated for Div. 2)
    Codeforces Round #487 (Div. 2)
    Codeforces Round #485
    codeforces Avito Code Challenge 2018
    MySQL索引知识面试题
    使用多线程优化复杂逻辑以及数据量多处理
    elasticsearch 和Ik 以及 logstash 同步数据库数据
    linux 安装elasticsearch步骤以及入的坑
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/9267633.html
Copyright © 2020-2023  润新知