DataTable转Json
2021-06-27 12:03
标签:var new tab app else ring pen table builder DataTable dt=XXXX; StringBuilder JsonStr = new StringBuilder(); DataTable转Json 标签:var new tab app else ring pen table builder 原文地址:http://www.cnblogs.com/chj929555796/p/7147506.html
if (dt.Rows.Count > 0)
{
JsonStr.Append("[");
for (int i = 0; i {
JsonStr.Append("{");
for (int j = 0; j {
if (j {
JsonStr.Append("\""+ dt.Columns[j].ColumnName.ToString() +"\":\"" + dt.Rows[i][j].ToString() + "\",");
}
else if (j == dt.Columns.Count-1)
{
JsonStr.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
}
}
if (i == dt.Rows.Count - 1)
{
JsonStr.Append("}");
}
else
{
JsonStr.Append("},");
}
}
JsonStr.Append("]");
}
var value = JsonStr.ToString();