C# JSON类

C# JSON类

using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Reflection;
using System;

namespace Sam.OA.Common
{
    /// 
    /// Json帮助类
    /// 使用前需引用开源项目类库:Newtonsoft.Json.dll
    /// 
    public sealed class JsonHelper
    {
        /// 
        /// 将对象序列化为json格式
        /// 
        /// 序列化对象
        /// json字符串
        public static string SerializeObjct(object obj)
        {            
            return JsonConvert.SerializeObject(obj);
        }
        /// 
        /// 解析JSON字符串生成对象实体
        /// 
        /// 实体类
        /// JSON字符串
        /// 
        public static T JsonConvertObject<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }
        /// 
        /// 解析JSON字符串生成对象实体
        /// 
        /// 对象类型
        /// json字符串
        /// 
        public static T DeserializeJsonToObject<T>(string json) where T:class
        {
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(json);
            object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
            T t = obj as T;
            return t;
        }
        /// 
        /// 解析JSON数组生成对象实体集合
        /// 
        /// 对象类型
        /// json数组
        /// 对象实体集合
        public static List<T> DeserializeJsonToList<T>(string json) where T : class
        {
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(json);
            object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
            List<T> list = obj as List<T>;
            return list;
        }
        /// 
        /// 将JSON转数组
        /// 用法:jsonArr[0]["xxxx"]
        /// 
        /// json字符串
        /// 
        public static JArray GetToJsonList(string json)
        {
            JArray jsonArr = (JArray)JsonConvert.DeserializeObject(json);
            return jsonArr;
        }
        /// 
        /// 将DataTable转换成实体类
        /// 
        /// 实体类
        /// DataTable
        /// 
        public static List<T> DtConvertToModel<T>(DataTable dt) where T : new()
        {
            List<T> ts = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                foreach (PropertyInfo pi in t.GetType().GetProperties())
                {
                    if (dt.Columns.Contains(pi.Name))
                    {
                        if (!pi.CanWrite) continue;
                        var value = dr[pi.Name];
                        if (value != DBNull.Value)
                        {
                            switch (pi.PropertyType.FullName)
                            {
                                case "System.Decimal":
                                    pi.SetValue(t, decimal.Parse(value.ToString()), null);
                                    break;
                                case "System.String":
                                    pi.SetValue(t, value.ToString(), null);
                                    break;
                                case "System.Int32":
                                    pi.SetValue(t, int.Parse(value.ToString()), null);
                                    break;
                                default:
                                    pi.SetValue(t, value, null);
                                    break;
                            }
                        }
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
    }
}

参考文章:
https://www.cnblogs.com/chenyanbin/p/11200415.html

你可能感兴趣的:(C#)