C#中DataTable转换JSON

时间:2021-09-09 03:31:30

#region 将DataTable转换为json
public string dt2json(DataTable dt)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
System.Collections.ArrayList dic = new System.Collections.ArrayList();
foreach (DataRow dr in dt.Rows)
{
System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
drow.Add(dc.ColumnName, dr[dc.ColumnName]);
}
dic.Add(drow);

}
//序列化
return jss.Serialize(dic);
}
#endregion

结伴旅游网www.jieberu.com

推推族www.tuituizu.com