• JsonParse类


    using System.Data;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using System;
    using System.Text;
    
    namespace es.Common
    {
        public class JsonParse : IDisposable
        {
            #region 释放资源
            bool dis;
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            protected virtual void Dispose(bool disposing)
            {
                if (dis) return;
                if (disposing)
                    dis = true;
            }
            ~JsonParse() { Dispose(false); }
            #endregion
            /// <summary>
            /// DataTable转换为json
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public string ToJson(DataTable dt)
            {
                if (dt != null && dt.Rows.Count > 0) return JsonConvert.SerializeObject(dt, new DataTableConverter(), new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" });
                else return null;
            }
            /// <summary>
            /// DataTable转换为json
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="format">时间格式:默认:“yyyy-MM-dd hh:mm:ss”</param>
            /// <returns></returns>
            public string ToJson(DataTable dt, string format)
            {
                format = string.IsNullOrEmpty(format) ? "yyyy-MM-dd hh:mm:ss" : format;
                if (dt != null && dt.Rows.Count > 0) return JsonConvert.SerializeObject(dt, new DataTableConverter(), new IsoDateTimeConverter { DateTimeFormat = format });
                else return null;
            }
            /// <summary>
            /// 表格形式转换为json
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="total"></param>
            /// <returns></returns>
            public string ToTablePage(DataTable dt, int total)
            {
                if (total > 0 && dt.Rows.Count > 0) return "{"total":" + total + ","content":" + this.ToJson(dt) + "}";
                else return null;
            }
            /// <summary>
            /// layui框架table模块数据转换为json
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="count"></param>
            /// <returns></returns>
            public string ToLayuiTable(DataTable dt, int count)
            {
                if (count > 0 && dt.Rows.Count > 0) return "{"code":0,"msg":"","count":" + count + ","data":" + this.ToJson(dt) + "}";
                else return "{"code":-1,"msg":"没有符合条件的记录","count":0,"data":[]}";
            }
            /// <summary>
            /// layui框架table模块数据转换为json
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="count"></param>
            /// <param name="format"></param>
            /// <returns></returns>
            public string ToLayuiTable(DataTable dt, int count, string format)
            {
                if (count > 0 && dt.Rows.Count > 0) return "{"code":0,"msg":"","count":" + count + ","data":" + this.ToJson(dt, format) + "}";
                else return "{"code":-1,"msg":"没有符合条件的记录","count":0,"data":[]}";
            }
            /// <summary>
            /// 获得带有children的json字符串,字段里必须有(id,pid)两个字段
            /// </summary>
            /// <param name="dt">DataTable</param>
            /// <param name="id">父级id</param>
            /// <returns></returns>
            public string ToChildren(DataTable dt, int id)
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow[] rows = dt.Select("pid=" + id);
                    if (rows.Length == 0) return string.Empty;
                    StringBuilder str = new StringBuilder();
                    foreach (DataRow row in rows)
                    {
                        str.Append("{");
                        for (int i = 0; i < row.Table.Columns.Count; i++)
                        {
                            if (row.Table.Columns[i].ColumnName != "pid")
                            {
                                if (i != 0) str.Append(",");
                                str.Append(""");
                                str.Append(row.Table.Columns[i].ColumnName);
                                str.Append("":"");
                                str.Append(row[i]);
                                str.Append(""");
                            }
                        }
                        if (ToChildren(dt, (int)row["id"]).Length > 0)
                        {
                            str.Append(","children":");
                            str.Append(ToChildren(dt, (int)row["id"]));
                            str.Append("},");
                        }
                        else
                        {
                            str.Append("},");
                        }
                    }
                    string json = str[str.Length - 1] == ',' ? str.ToString(0, str.Length - 1) : str.ToString();
                    return "[" + json + "]";
                }
                else
                {
                    return null;
                }
            }
        }
    }
  • 相关阅读:
    Linux下C编程入门(1)
    Git 常用命令速查表
    Git Cheat Sheet 中文版
    Linux 在一个命令行上执行多个命令
    一个奇怪的错误的警示
    模块化编程实例(一)
    含有指针变量的结构体的指针的应用
    iOS 开发加密做法
    关于设置shadowPath的重要性
    关于设置shadowPath的重要性
  • 原文地址:https://www.cnblogs.com/hllive/p/7658607.html
Copyright © 2020-2023  润新知