DataTable to Json
/// <summary> /// DataTable to json /// </summary> /// <param name="jsonName">返回json的名称</param> /// <param name="dt">转换成json的表</param> /// <returns></returns> public static string DataTableToJson(string jsonName, System.Data.DataTable dt) { StringBuilder Json = new StringBuilder(); Json.Append("{\"" + jsonName + "\":["); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { Json.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\""); if (j < dt.Columns.Count - 1) { Json.Append(","); } } Json.Append("}"); if (i < dt.Rows.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); }