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;
       }

  • 相关阅读:
    JavaSE-方法覆盖的注意事项
    underscore.js源码研究(4)
    underscore.js源码研究(3)
    移动端font-size适配方案
    控制台引入想要的库
    页面布局与编写(续3)
    underscore.js源码研究(2)
    全屏使用swiper.js过程中遇到的坑
    模块加载
    underscore.js源码研究(1)
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3231074.html
Copyright © 2011-2022 走看看