• 后台转换JSON格式。ToJson


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.Data;
    using System.Collections;
    
    namespace DotNet.Utilities
    {
        /// <summary>
        /// 转换Json格式帮助类
        /// </summary>
        public class JsonHelper
        {
         /// <summary>
            /// DataTable转Json
            /// </summary>
            /// <param name="dt">table数据集</param>
            /// <param name="dtName">json名</param>
            /// <returns>例子{"JSON":[{"STO_ID":"26","STO_NO":"SEORD14041500014","PRO_NO":"SEORD14041500012","TOTAL_MONEY":"4.0000"}]}</returns>
            public static string DataTableToJson(DataTable dt, string dtName)
            {
                try
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("{"");
                    sb.Append(dtName);
                    sb.Append("":[");
                    if (DataTableHelper.IsExistRows(dt))
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            sb.Append("{");
                            foreach (DataColumn dc in dr.Table.Columns)
                            {
                                sb.Append(""");
                                sb.Append(dc.ColumnName);
                                sb.Append("":"");
                                if (dr[dc] != null && dr[dc] != DBNull.Value && dr[dc].ToString() != "")
                                    sb.Append(dr[dc]).Replace("\", "/");
                                else
                                    sb.Append("");
                                sb.Append("",");
                            }
                            sb = sb.Remove(sb.Length - 1, 1);
                            sb.Append("},");
                        }
                        sb = sb.Remove(sb.Length - 1, 1);
                    }
                    sb.Append("]}");
                    return JsonCharFilter(sb.ToString());
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex.Message);
                    return "";
                }
            }
            /// <summary>
            /// Hashtable表转JSON
            /// </summary>
            /// <param name="data"></param>
            /// <param name="dtName">JSON名,例:JSON</param>
            /// <returns></returns>
            public static string HashtableToJson(Hashtable data, string dtName)
            {
                try
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("{"");
                    sb.Append(dtName);
                    sb.Append("":[{");
                    foreach (object key in data.Keys)
                    {
                        object value = data[key];
                        sb.Append(""");
                        sb.Append(key);
                        sb.Append("":"");
                        if (!String.IsNullOrEmpty(value.ToString()) && value != DBNull.Value)
                        {
                            sb.Append(value).Replace("\", "/");
                        }
                        else
                        {
                            sb.Append(" ");
                        }
                        sb.Append("",");
                    }
                    sb = sb.Remove(sb.Length - 1, 1);
                    sb.Append("}]}");
                    return JsonCharFilter(sb.ToString());
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex.Message);
                    return "";
                }
            }
            /// <summary>
            /// 表格返回JSON
            /// </summary>
            /// <param name="dt">数据行</param>
            /// <param name="pageIndex">当前页面</param>
            /// <param name="pqGrid_Sort">要显示字段</param>
            /// <param name="count"></param>
            /// <returns></returns>
            public static string PqGridPageJson(DataTable dt, int pageIndex, string pqGrid_Sort, int count)
            {
                try
                {
                    string[] Sort_Field = pqGrid_Sort.Split(',');
                    StringBuilder sb = new StringBuilder();
                    sb.Append("{");
                    sb.Append(""totalRecords": " + count + ",");
                    sb.Append(""curPage": " + pageIndex + ",");
                    sb.Append(""data": [");
                    if (DataTableHelper.IsExistRows(dt))
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            sb.Append("[");
                            foreach (string item in Sort_Field)
                            {
                                sb.Append(""");
                                if (dr[item] != null && dr[item] != DBNull.Value && dr[item].ToString() != "")
                                    sb.Append(dr[item]);
                                else
                                    sb.Append("");
                                sb.Append("",");
                            }
                            sb = sb.Remove(sb.Length - 1, 1);
                            sb.Append("],");
                        }
                        sb = sb.Remove(sb.Length - 1, 1);
                        sb.Append("]");
                        sb.Append("}");
                    }
                    return sb.ToString();
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex.Message);
                    return "";
                }
            }
            /// <summary>
            /// 表格返回JSON
            /// </summary>
            /// <param name="pqGrid_Sort">要显示字段</param>
            /// <param name="count"></param>
            /// <returns></returns>
            public static string PqGridJson(DataTable dt, string pqGrid_Sort)
            {
                try
                {
                    string[] Sort_Field = pqGrid_Sort.Split(',');
                    StringBuilder sb = new StringBuilder();
                    if (DataTableHelper.IsExistRows(dt))
                    {
                        sb.Append("[");
                        foreach (DataRow dr in dt.Rows)
                        {
                            sb.Append("[");
                            foreach (string item in Sort_Field)
                            {
                                sb.Append(""");
                                if (dr[item] != null && dr[item] != DBNull.Value && dr[item].ToString() != "")
                                    sb.Append(dr[item]);
                                else
                                    sb.Append("");
                                sb.Append("",");
                            }
                            sb = sb.Remove(sb.Length - 1, 1);
                            sb.Append("],");
                        }
                        sb = sb.Remove(sb.Length - 1, 1);
                        sb.Append("]");
                    }
                    return sb.ToString();
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex.Message);
                    return "";
                }
            }
            /// <summary>
            /// 表格返回JSON
            /// </summary>
            /// <param name="pqGrid_Sort">要显示字段</param>
            /// <param name="count"></param>
            /// <returns></returns>
            public static string GridTable(DataTable dt, string pqGrid_Sort)
            {
                try
                {
                    string[] Sort_Field = pqGrid_Sort.Split(',');
                    StringBuilder sb = new StringBuilder();
                    if (DataTableHelper.IsExistRows(dt))
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            sb.Append("<tr>");
                            foreach (string item in Sort_Field)
                            {
                                if (dr[item] != null && dr[item] != DBNull.Value && dr[item].ToString() != "")
                                    sb.Append("<td>" + dr[item] + "</td>");
                                else
                                    sb.Append("<td></td>");
                            }
                            sb.Append("</tr>");
                        }
                    }
                    return sb.ToString();
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex.Message);
                    return "";
                }
            }
            /// <summary>  
            /// Json特符字符过滤
            /// </summary>  
            /// <param name="sourceStr">要过滤的源字符串</param>  
            /// <returns>返回过滤的字符串</returns>  
            private static string JsonCharFilter(string sourceStr)
            {
                return sourceStr;
            }
    
        }
    }
    
    
  • 相关阅读:
    tif文件导入postgresql
    与你相遇好幸运,使用redis设置定时任务
    用sinopia搭建npm私服
    Postman设置Header不生效问题
    iOS 动态加载LaunchScreen上的图片
    iOS 封装一个带复制功能的UILabel
    ios开发文字排版,段落排版,富文本
    iOS使用hitTest和loadView处理UIView事件传递
    iOS 更改状态栏颜色和隐藏状态栏
    iOS scrollView嵌套tableView的手势冲突解决方案
  • 原文地址:https://www.cnblogs.com/lengv10/p/3665883.html
Copyright © 2020-2023  润新知