第2个回答 2012-01-11
public static string DataTableToJson(DataTable dt, string JsonName)
{
try
{
if (dt == null)
{
return "DataTable Is Null ,So I Can't Do It To Json!";
}
string josn = "\"" + JsonName + "\":[";
string temp = "";
for (int j = 0; j < dt.Rows.Count; j++)
{
temp = temp + "{";
for (int i = 0; i < dt.Columns.Count; i++)
{
temp += "\"" + dt.Columns[i].ColumnName.ToLower() + "\":\"" + dt.Rows[j][i] + "\"";
if (i != dt.Columns.Count - 1)
{
temp = temp + ",";
}
}
if (j == dt.Rows.Count - 1)
{
temp = temp + "}";
}
else
{
temp = temp + "},";
}
}
josn = josn + temp + "]";
return josn;
}
catch (Exception ex)
{
return "Codeing is Error----" + ex.ToString();
}
}
datatable 转json、