• 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("]");
            }

    记录一下,共同学习

  • 相关阅读:
    mybatis入参错误:There is no getter for property named ‘status‘ in ‘class java.lang.Integer‘
    JAVA程序员面试笔试题(一)
    Java8新特性LocalDateTime获取周几
    linux常用命令记录 screen
    ubuntu 19.04 + lenovo-xiaoxin-I2000 触摸板右键单击无法使用
    华为交换路由常用命令
    centos7常用软件
    一般网络延迟高的原因
    华为防火墙进程&简单配置
    私网互联(本质是三层路由)
  • 原文地址:https://www.cnblogs.com/hqbird/p/1583287.html
Copyright © 2020-2023  润新知