• DataTable解析成JSON方法说明


    一下是解析DataTable成JSON数据格式的函数方法:

    Code
    /// <summary>
            
    /// 将数据表转换成JSON类型串
            
    /// </summary>
            
    /// <param name="dt">要转换的数据表</param>
            
    /// <param name="dispose">数据表转换结束后是否dispose掉</param>
            
    /// <returns></returns>
            public static StringBuilder DataTableToJSON(System.Data.DataTable dt, bool dt_dispose)
            {
                StringBuilder stringBuilder 
    = new StringBuilder();
                stringBuilder.Append(
    "[");

                
    //数据表字段名和类型数组
                string[] dt_field = new string[dt.Columns.Count];
                
    int i = 0;
                
    string formatStr = "{{";
                
    string fieldtype = "";
                
    foreach (System.Data.DataColumn dc in dt.Columns)
                {
                    dt_field[i] 
    = dc.Caption.ToLower().Trim();
                    formatStr 
    += '"' + dc.Caption.ToLower().Trim() + '"' + ":";
                    fieldtype 
    = dc.DataType.ToString().Trim().ToLower();
                    
    if (fieldtype.IndexOf("int"> 0 || fieldtype.IndexOf("deci"> 0 ||
                        fieldtype.IndexOf(
    "floa"> 0 || fieldtype.IndexOf("doub"> 0 ||
                        fieldtype.IndexOf(
    "bool"> 0)
                    {
                        formatStr 
    += "{" + i + "}";
                    }
                    
    else
                    {
                        formatStr 
    += '"' + "{" + i + "}" + '"';
                    }
                    formatStr 
    += ",";
                    i
    ++;
                }

                
    if (formatStr.EndsWith(","))
                {
                    formatStr 
    = formatStr.Substring(0, formatStr.Length - 1);//去掉尾部","号
                }
                formatStr 
    += "}},";

                i 
    = 0;
                
    object[] objectArray = new object[dt_field.Length];
                
    foreach (System.Data.DataRow dr in dt.Rows)
                {

                    
    foreach (string fieldname in dt_field)
                    {   
    //对 \ , ' 符号进行转换 
                        objectArray[i] = dr[dt_field[i]].ToString().Trim().Replace("\\""\\\\").Replace("'""\\'").Replace("\n","");
                        
    switch (objectArray[i].ToString())
                        {
                            
    case "True":
                                {
                                    objectArray[i] 
    = "true"break;
                                }
                            
    case "False":
                                {
                                    objectArray[i] 
    = "false"break;
                                }
                            
    defaultbreak;
                        }
                        i
    ++;
                    }
                    i 
    = 0;
                    stringBuilder.Append(
    string.Format(formatStr, objectArray));
                }
                
    if (stringBuilder.ToString().EndsWith(","))
                {
                    stringBuilder.Remove(stringBuilder.Length 
    - 11);//去掉尾部","号
                }

                
    if (dt_dispose)
                {
                    dt.Dispose();
                }
                
    return stringBuilder.Append("]");
            }

    记录一下,共同学习

  • 相关阅读:
    516. Longest Palindromic Subsequence
    31. Next Permutation
    572. Subtree of Another Tree
    vue中无法监听到子组件的事件的问题
    vue中注意watch的执行顺序
    vue中路由懒加载浅析
    关于typescript 报错问题
    深入理解Typescript中文版
    vue中的高阶组件
    vue中的mixins
  • 原文地址:https://www.cnblogs.com/hqbird/p/1583287.html
Copyright © 2020-2023  润新知