方法:
public static string GetJsonByDataset(DataSet ds)
{
if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
{
return null;
}
StringBuilder sb = new StringBuilder();
foreach (DataTable dt in ds.Tables)
{
sb.Append(string.Format("["));
foreach (DataRow dr in dt.Rows)
{
sb.Append("{");
for (int i = 0; i < dr.Table.Columns.Count; i++)
{
sb.AppendFormat("\"{0}\":\"{1}\",", dr.Table.Columns[i].ColumnName, dr[i].ToString());
}
sb.Remove(sb.ToString().LastIndexOf(‘,‘), 1);
sb.Append("},");
}
sb.Remove(sb.ToString().LastIndexOf(‘,‘), 1);
sb.Append("]");
}
return sb.ToString();
}
时间: 2024-10-11 07:19:44