public class JsonParseService { /// <summary> /// dataset转Json /// </summary> /// <param name="ds"></param> /// <returns></returns> public static string DatasetToJson(System.Data.DataSet ds) { StringBuilder json = new StringBuilder(); json.Append("{\"Tables\":"); json.Append("["); foreach (System.Data.DataTable dt in ds.Tables) { json.Append(DataTableToJson(dt)); json.Append(","); } json.Remove(json.Length - 1, 1); json.Append("]"); json.Append("}"); return json.ToString(); } /// <summary> /// table转json /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string DataTableToJson(System.Data.DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{\"Name\":\"" + dt.TableName + "\",\"Rows"); 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().Replace("\"", "\\\"")); jsonBuilder.Append("\","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("]"); jsonBuilder.Append("}"); return jsonBuilder.ToString(); } }
ASP.NET讨论qq群 :171560784 欢迎各位加入
|
请发表评论