/// <summary>
/// 将datatable 转换成json数据格式
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private string DataTableToJson(DataTable dt)
{
StringBuilder strJson = new StringBuilder();
strJson.Append("[");
for (int i = 0; i < dt.Rows.Count; i++)
{
strJson.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
strJson.Append("\"" + dt.Columns[j].ColumnName.Trim() + "\"");
strJson.Append(":");
strJson.Append("\"" + dt.Rows[i][j].ToString().Trim() + "\"");
if ((j + 1) != dt.Columns.Count)
strJson.Append(",");
}
strJson.Append("}");
if ((i + 1) != dt.Rows.Count)
strJson.Append(",");
}
strJson.Append("]");
return strJson.ToString();
}
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)