• JSONHelper带转换datatable


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Runtime.Serialization.Json;
    using System.IO;
    using System.Text;
    using System.Web.Script.Serialization;
    using System.Data;
    
    /// <summary>
    /// JSON序列化和反序列化辅助类
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// 序列化方法(带分页)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string Serialize(DataTable dt)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    result.Add(dc.ColumnName, dr[dc].ToString());
                }
                list.Add(result);
            }
            int count = 0;
            try
            {
                count = Convert.ToInt32(dt.TableName);
            }
            catch (System.Exception ex)
            {
                count = dt.Rows.Count;
            }
            string strReturn = "";
            if (count == 0)
            {
                strReturn = "{'totalCount':0,'data':[]}";
            }
            else
            {
                strReturn = ConventToJson(list, count);
            }
            return strReturn;
        }
    
        /// <summary>
        /// 转换为JSON对象
        /// </summary>
        /// <returns></returns>
        public static string ConventToJson<T>(List<T> list, int count)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string strJson = serializer.Serialize(list);
            strJson = strJson.Substring(1);
            strJson = strJson.Insert(0, "{totalCount:" + count + ",data:[");
            strJson += "}";
    
            return strJson;
        }
    
        /// <summary>
        /// 不需要分页
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="flag">false</param>
        /// <returns></returns>
        public static string Serialize(DataTable dt, bool flag)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    result.Add(dc.ColumnName, dr[dc].ToString());
                }
                list.Add(result);
            }
            return serializer.Serialize(list); ;
        }
    
    
    
        /// <summary>
        /// JSON序列化
        /// </summary>
        public static string JsonSerializer<T>(T t)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, t);
            string jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }
    
        /// <summary>
        /// JSON反序列化
        /// </summary>
        public static T JsonDeserialize<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T obj = (T)ser.ReadObject(ms);
            return obj;
        }
    }
    

      

  • 相关阅读:
    noip欢乐赛10.24 分火腿
    noip2014 无线网络发射器选址/wireless.
    noip2012 借教室 线段树最小值做法
    Codevs1021题解---SPFA+路径记录
    Vijos1448题解---线段树+括号法
    Vijos1425题解---栈
    Codevs1022题解---匈牙利算法
    人们总要为曾经的年轻买单
    2017-10-26
    2017-10-24LCA
  • 原文地址:https://www.cnblogs.com/lierjie/p/4517082.html
Copyright © 2020-2023  润新知