zoukankan      html  css  js  c++  java
  • 将datatable转化为Json格式字符串

     /// 将datatable转化为Json格式字符串
            /// </summary>  
            /// <param name="dt"></param>  
            /// <returns></returns>  
            public static string CreateJsonToDataTable(System.Data.DataTable dt)
            {
                StringBuilder JsonString = new StringBuilder();

                if (dt != null && dt.Rows.Count > 0)
                {
                    JsonString.Append("[");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        JsonString.Append("{ ");
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (j < dt.Columns.Count - 1)
                            {
                                JsonString.Append("\""+dt.Columns[j].ColumnName.ToString() +"\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                            }
                            else if (j == dt.Columns.Count - 1)
                            {
                                JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                            }
                        }
                        if (i == dt.Rows.Count - 1)
                        {
                            JsonString.Append("} ");
                        }
                        else
                        {
                            JsonString.Append("}, ");
                        }
                    }
                    JsonString.Append("]");
                    return JsonString.ToString();
                }
                return null;
            }
  • 相关阅读:
    postman接口测试01_下载安装
    Jenkins持续集成_05_参数化构建
    IntelliJ 中设置与Eclipse中 Ctrl+1 功能类似的快捷键
    JAVA 内部类
    Adobe Air 现在可支持X86的移动设备
    开发一些自动编写代码的工具可以增加效率吗?
    制作 Flex 悬停提示帮助文档(转)
    没什么好说的
    JDK,Tomcat,Maven,Mysql环境变量配置
    神器三、Android端神器东皇钟:天界之门
  • 原文地址:https://www.cnblogs.com/zxh0208/p/2118551.html
Copyright © 2011-2022 走看看