///<summary>
/// DataTable转成Json
///</summary>
///<param name="dt"></param>
///<returns></returns>
publicstaticstring DataTableToJson(DataTable dt)
{
StringBuilder jsonBuilder = newStringBuilder();
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("]");
return jsonBuilder.ToString();
}
DtatTable转成Json格式,布布扣,bubuko.com
原文:http://www.cnblogs.com/XiaoHunZhen/p/3733467.html