• 序列化泛型 反序列化JSON


    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Web.Script.Serialization;
    
    using System.Reflection;
    
    using System.Data;
    
    namespace Business.Util
    {
    
        public class JsonHelper
        {
    
            public static T ConvertToEntity<T>(string sJson)
            {
    
                if (string.IsNullOrEmpty(sJson))
                {
    
                    throw new Exception("转换的JSON数据不能为空");
    
                }
    
                JavaScriptSerializer js = new JavaScriptSerializer();
    
                var objDic = js.Deserialize<T>(sJson);
    
                return objDic;
    
            }
    
            ///<summary>
    
            ///将DataTable转换成Json
    
            ///</summary>
    
            ///<param name="jsonName"></param>
    
            ///<param name="dt"></param>
    
            ///<returns></returns>
    
            public static string DataTableToJson(string jsonName, DataTable dt)
            {
    
                if (dt == null || dt.Rows.Count == 0)
                {
    
                    return "";
    
                }
    
                else
                {
    
                    StringBuilder Json = new StringBuilder();
    
                    Json.Append("{\"" + jsonName + "\":[");
    
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
    
                        Json.Append("{");
    
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
    
                            Json.Append("\"" +
                                        dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
    
                            if (j < dt.Columns.Count - 1)
                            {
    
                                Json.Append(",");
    
                            }
    
                        }
    
                        Json.Append("}");
    
                        if (i < dt.Rows.Count - 1)
                        {
    
                            Json.Append(",");
    
                        }
    
                    }
    
                    Json.Append("]}");
    
                    return Json.ToString();
    
                }
    
            }
    
            ///<summary>
    
            ///将泛型IList转换成Json
    
            ///</summary>
    
            ///<typeparam name="T"></typeparam>
    
            ///<param name="jsonName"></param>
    
            ///<param name="IL"></param>
    
            ///<returns></returns>
    
            public static string ObjectToJson<T>(string jsonName, IList<T> IL)
            {
    
                StringBuilder Json = new StringBuilder();
    
                Json.Append("{\"" + jsonName + "\":[");
    
                if (IL.Count > 0)
                {
    
                    for (int i = 0; i < IL.Count; i++)
                    {
    
                        T obj = Activator.CreateInstance<T>();
    
                        Type type = obj.GetType();
    
                        PropertyInfo[] pis = type.GetProperties();
    
                        Json.Append("{");
    
                        for (int j = 0; j < pis.Length; j++)
                        {
    
                            Json.Append("\"" + pis[j].Name.ToString()
                                        + "\":\"" +
                                        pis[j].GetValue(IL[i], null) + "\"");
    
                            if (j < pis.Length - 1)
                            {
    
                                Json.Append(",");
    
                            }
    
                        }
    
                        Json.Append("}");
    
                        if (i < IL.Count - 1)
                        {
    
                            Json.Append(",");
    
                        }
    
                    }
    
                }
    
                Json.Append("]}");
    
                return Json.ToString();
    
            }
    
        }
    
    }
    


  • 相关阅读:
    Vue中事件委托的使用
    java提取每个汉字的首字母
    想把大脑存进电脑,我为什么要写博客
    CF 1606 D题题解
    js前端 音频波形图像展示
    js前端 仪表盘实现
    js前端 bootstrap select的使用
    UOS系统维护命令
    linux 打印机管理常用命令
    linux 调用shell命令
  • 原文地址:https://www.cnblogs.com/dashi/p/4034652.html
Copyright © 2020-2023  润新知