当前位置: 移动技术网 > IT编程>开发语言>.net > .NET DataTable转化为json格式

.NET DataTable转化为json格式

2018年03月14日  | 移动技术网IT编程  | 我要评论

颜健鸥,哈尔滨建站多少钱,网上药店哪个好

标准的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;
   }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网