jsonHelper帮助类

使用前,需引用开源项目类using Newtonsoft.Json

链接:https://pan.baidu.com/s/1htK784XyRCl2XaGGM7RtEg
提取码:gs2n

  1 using System.Collections.Generic;
  2 using System.IO;
  3 using Newtonsoft.Json;
  4 using Newtonsoft.Json.Linq;
  5 using System.Data;
  6 using System.Reflection;
  7 using System;
  8 
  9 namespace Sam.OA.Common
 10 {
 11     /// <summary>
 12     /// Json帮助类
 13     /// 作者:陈彦斌
 14     /// 最后更新日期:2019年9月28日20:11:36
 15     /// 使用前需引用开源项目类库:Newtonsoft.Json.dll
 16     /// </summary>
 17     public sealed class JsonHelper
 18     {
 19         /// <summary>
 20         /// 将实体类序列化字符串
 21         /// </summary>
 22         /// <typeparam name="T">实体类</typeparam>
 23         /// <param name="t"></param>
 24         /// <returns></returns>
 25         public static string SerializeObject<T>(T t)
 26         {
 27             try
 28             {
 29                 return JsonConvert.SerializeObject(t);
 30             }
 31             catch (Exception ex)
 32             {
 33                 throw ex;
 34             }
 35         }
 36         /// <summary>
 37         /// 将对象序列化为json格式
 38         /// </summary>
 39         /// <param name="obj">序列化对象</param>
 40         /// <returns>json字符串</returns>
 41         public static string SerializeObjct(object obj)
 42         {
 43             return JsonConvert.SerializeObject(obj);
 44         }
 45         /// <summary>
 46         /// 解析JSON字符串生成对象实体
 47         /// </summary>
 48         /// <typeparam name="T">实体类</typeparam>
 49         /// <param name="json">JSON字符串</param>
 50         /// <returns></returns>
 51         public static T JsonConvertObject<T>(string json)
 52         {
 53             return JsonConvert.DeserializeObject<T>(json);
 54         }
 55         /// <summary>
 56         /// 解析JSON字符串生成对象实体
 57         /// </summary>
 58         /// <typeparam name="T">对象类型</typeparam>
 59         /// <param name="json">json字符串</param>
 60         /// <returns></returns>
 61         public static T DeserializeJsonToObject<T>(string json) where T : class
 62         {
 63             JsonSerializer serializer = new JsonSerializer();
 64             StringReader sr = new StringReader(json);
 65             object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
 66             T t = obj as T;
 67             return t;
 68         }
 69         /// <summary>
 70         /// 解析JSON数组生成对象实体集合
 71         /// </summary>
 72         /// <typeparam name="T">对象类型</typeparam>
 73         /// <param name="json">json数组</param>
 74         /// <returns>对象实体集合</returns>
 75         public static List<T> DeserializeJsonToList<T>(string json) where T : class
 76         {
 77             JsonSerializer serializer = new JsonSerializer();
 78             StringReader sr = new StringReader(json);
 79             object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
 80             List<T> list = obj as List<T>;
 81             return list;
 82         }
 83         /// <summary>
 84         /// 将JSON转数组
 85         /// 用法:jsonArr[0]["xxxx"]
 86         /// </summary>
 87         /// <param name="json">json字符串</param>
 88         /// <returns></returns>
 89         public static JArray GetToJsonList(string json)
 90         {
 91             JArray jsonArr = (JArray)JsonConvert.DeserializeObject(json);
 92             return jsonArr;
 93         }
 94         /// <summary>
 95         /// 将DataTable转换成实体类
 96         /// </summary>
 97         /// <typeparam name="T">实体类</typeparam>
 98         /// <param name="dt">DataTable</param>
 99         /// <returns></returns>
100         public static List<T> DtConvertToModel<T>(DataTable dt) where T : new()
101         {
102             List<T> ts = new List<T>();
103             foreach (DataRow dr in dt.Rows)
104             {
105                 T t = new T();
106                 foreach (PropertyInfo pi in t.GetType().GetProperties())
107                 {
108                     if (dt.Columns.Contains(pi.Name))
109                     {
110                         if (!pi.CanWrite) continue;
111                         var value = dr[pi.Name];
112                         if (value != DBNull.Value)
113                         {
114                             switch (pi.PropertyType.FullName)
115                             {
116                                 case "System.Decimal":
117                                     pi.SetValue(t, decimal.Parse(value.ToString()), null);
118                                     break;
119                                 case "System.String":
120                                     pi.SetValue(t, value.ToString(), null);
121                                     break;
122                                 case "System.Int32":
123                                     pi.SetValue(t, int.Parse(value.ToString()), null);
124                                     break;
125                                 default:
126                                     pi.SetValue(t, value, null);
127                                     break;
128                             }
129                         }
130                     }
131                 }
132                 ts.Add(t);
133             }
134             return ts;
135         }
136     }
137 }
posted @ 2019-07-14 16:30  陈彦斌  阅读(1208)  评论(0编辑  收藏  举报