14
2013
07

.NET DataTable转化为json格式

.Net Code:

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



版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。