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

  • 相关阅读:
    iphone那些事儿
    【转】我面试过最出色的项目主管,入职半年就离职了。
    net::ERR_ABORTED 404 (Not Found)
    四大名著
    测试心理状态
    typescript那些事儿
    flexbox父盒子flex-direction属性
    flexbox父盒子align-content属性
    flexbox父盒子flex-wrap属性
    flexbox父盒子align-items属性
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3231074.html
Copyright © 2011-2022 走看看