我带来了很多数据,Table
所以我希望它在返回时将其转换为 json。所以我尝试如下
public static string DataSetToJSON(DataSet dset)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in dset.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in dset.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
但我在 dset.Rows
System.Data.DataSet中不包含的定义
Rows
....
在检查和做了一些研究之后,也在SLAKS
我的指导下,我终于做到了。
public static string DataSetToJSON(DataSet ds)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, dr[col]);
}
parentRow.Add(childRow);
}
}
return jsSerializer.Serialize(parentRow);
}
由于SLAKS
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句