DataTable或DataSet與JSON互轉
阿新 • • 發佈:2019-01-26
通過AJAX非同步減少網路內容傳輸,而JSON則可以把傳輸內容縮減到純資料;然後利用jQuery內建的AJAX功能直接獲得JSON格式的資料;在客戶端直接繫結到資料控制元件裡面,從而達到最優。 #region dataTable轉換成Json格式 /// <summary> /// dataTable轉換成Json格式 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string DataTable2Json(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{\""); jsonBuilder.Append(dt.TableName.ToString()); jsonBuilder.Append("\":["); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append("\""); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("\":\""); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("\","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("]"); jsonBuilder.Append("}"); return jsonBuilder.ToString(); } #endregion dataTable轉換成Json格式 #region DataSet轉換成Json格式 /// <summary> /// DataSet轉換成Json格式 /// </summary> /// <param name="ds">DataSet</param> /// <returns></returns> public static string Dataset2Json(DataSet ds) { StringBuilder json = new StringBuilder(); foreach (DataTable dt in ds.Tables) { json.Append("{\""); json.Append(dt.TableName); json.Append("\":"); json.Append(DataTable2Json(dt)); json.Append("}"); } return json.ToString(); } #endregion
今天又發現了有更好的轉換方法,具體如下 :
public class ConventDataTableToJson { /// <summary> /// 序列化方法(帶分頁) /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string Serialize(DataTable dt) { List<Dictionary<string, object>> list = new List<Dictionary<string, object>>(); foreach (DataRow dr in dt.Rows) { Dictionary<string, object> result = new Dictionary<string, object>(); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc].ToString()); } list.Add(result); } int count = 0; try { count = Convert.ToInt32(dt.TableName); } catch (System.Exception ex) { count = dt.Rows.Count; } string strReturn = ""; if (count == 0) { strReturn = "{\"totalCount\":0,\"data\":[]}"; } else { strReturn = ConventToJson(list, count); } return strReturn; } /// <summary> /// 轉換為JSON物件 /// </summary> /// <returns></returns> public static string ConventToJson<T>(List<T> list, int count) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string strJson = serializer.Serialize(list); strJson = strJson.Substring(1); strJson = strJson.Insert(0, "{totalCount:" + count + ",data:["); strJson += "}"; return strJson; } /// <summary> /// 不需要分頁 /// </summary> /// <param name="dt"></param> /// <param name="flag">false</param> /// <returns></returns> public static string Serialize(DataTable dt,bool flag) { JavaScriptSerializer serializer = new JavaScriptSerializer(); List<Dictionary<string, object>> list = new List<Dictionary<string, object>>(); foreach (DataRow dr in dt.Rows) { Dictionary<string, object> result = new Dictionary<string, object>(); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc].ToString()); } list.Add(result); } return serializer.Serialize(list); ; } }