- 通过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); ;
- }