DataTable转换json

原创 Laughing  2017-06-30 21:27  阅读 67 次 评论 0 条
  1. public class DataTableConvertJson    
  2.     {    
  3.   
  4.         #region dataTable转换成Json格式    
  5.         /// <summary>      
  6.         /// dataTable转换成Json格式      
  7.         /// </summary>      
  8.         /// <param name="dt"></param>      
  9.         /// <returns></returns>      
  10.         public static string DataTable2Json(DataTable dt)    
  11.         {    
  12.             StringBuilder jsonBuilder = new StringBuilder();    
  13.             jsonBuilder.Append("{\"");    
  14.             jsonBuilder.Append(dt.TableName);    
  15.             jsonBuilder.Append("\":[");    
  16.             jsonBuilder.Append("[");    
  17.             for (int i = 0; i < dt.Rows.Count; i++)    
  18.             {    
  19.                 jsonBuilder.Append("{");    
  20.                 for (int j = 0; j < dt.Columns.Count; j++)    
  21.                 {    
  22.                     jsonBuilder.Append("\"");    
  23.                     jsonBuilder.Append(dt.Columns[j].ColumnName);    
  24.                     jsonBuilder.Append("\":\"");    
  25.                     jsonBuilder.Append(dt.Rows[i][j].ToString());    
  26.                     jsonBuilder.Append("\",");    
  27.                 }    
  28.                 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);    
  29.                 jsonBuilder.Append("},");    
  30.             }    
  31.             jsonBuilder.Remove(jsonBuilder.Length - 1, 1);    
  32.             jsonBuilder.Append("]");    
  33.             jsonBuilder.Append("}");    
  34.             return jsonBuilder.ToString();    
  35.         }    
  36.   
  37.         #endregion dataTable转换成Json格式    
  38.         #region DataSet转换成Json格式    
  39.         /// <summary>      
  40.         /// DataSet转换成Json格式      
  41.         /// </summary>      
  42.         /// <param name="ds">DataSet</param>     
  43.         /// <returns></returns>      
  44.         public static string Dataset2Json(DataSet ds)    
  45.         {    
  46.             StringBuilder json = new StringBuilder();    
  47.     
  48.             foreach (DataTable dt in ds.Tables)    
  49.             {    
  50.                 json.Append("{\"");    
  51.                 json.Append(dt.TableName);    
  52.                 json.Append("\":");    
  53.                 json.Append(DataTable2Json(dt));    
  54.                 json.Append("}");    
  55.             } return json.ToString();    
  56.         }    
  57.         #endregion    
  58.     
  59.         /// <summary>    
  60.         /// Msdn    
  61.         /// </summary>    
  62.         /// <param name="jsonName"></param>    
  63.         /// <param name="dt"></param>    
  64.         /// <returns></returns>    
  65.         public static string DataTableToJson(string jsonName, DataTable dt)    
  66.         {    
  67.             StringBuilder Json = new StringBuilder();    
  68.             Json.Append("{\"" + jsonName + "\":[");    
  69.             if (dt.Rows.Count > 0)    
  70.             {    
  71.                 for (int i = 0; i < dt.Rows.Count; i++)    
  72.                 {    
  73.                     Json.Append("{");    
  74.                     for (int j = 0; j < dt.Columns.Count; j++)    
  75.                     {    
  76.                         Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");    
  77.                         if (j < dt.Columns.Count - 1)    
  78.                         {    
  79.                             Json.Append(",");    
  80.                         }    
  81.                     }    
  82.                     Json.Append("}");    
  83.                     if (i < dt.Rows.Count - 1)    
  84.                     {    
  85.                         Json.Append(",");    
  86.                     }    
  87.                 }    
  88.             }    
  89.             Json.Append("]}");    
  90.             return Json.ToString();    
  91.         }    
  92.     }    
本文地址:https://www.lisen.me/the-datatable-convert-json.html
版权声明:本文为原创文章,版权归 木子网 所有,欢迎分享本文,转载请保留出处!

发表评论


表情