zoukankan      html  css  js  c++  java
  • .NET DataTable转化为json格式

    标准的json用“分隔,不用' 

    public static string DataSetToJson(DataTable dt)

       {
           string json = string.Empty;
           try
           {
               if (dt==null||dt.Rows.Count == 0)
               {
                   return "";
               }
               json = "{";
               json += "'table" + 1 + "':[";
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   json += "{";
                   for (int j = 0; j < dt.Columns.Count; j++)
                   {
                       json += "'" + dt.Columns[j].ColumnName + "':'" + dt.Rows[i][j].ToString() + "'";
                       if (j != dt.Columns.Count - 1)
                       {
                           json += ",";
                       }
                   }
                   json += "}";
                   if (i != dt.Rows.Count - 1)
                   {
                       json += ",";
                   }
               }
               json += "]";
               json += "}";
           }
           catch (Exception ex)
           {

               throw new Exception(ex.Message);
           }
           return json;
       }

  • 相关阅读:
    自动轮播
    哈夫曼树的应用-金条划分
    计算两个日期相差的天数
    数据结构之算术表达式
    动态规划-矩阵最短路径
    动态规划-换钱最少货币数
    字母数字密码破解
    荷兰国旗问题
    集合并集
    进制数位幸运数
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3231074.html
Copyright © 2011-2022 走看看