如何将datatable转换为json

2024-12-27 09:15:13
推荐回答(1个)
回答1:

/// 将datatable转换为json
///
/// Dt
/// JSON字符串
public static string Dtb2Json(DataTable dtb)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
System.Collections.ArrayList dic = new System.Collections.ArrayList();
foreach (DataRow dr in dtb.Rows)
{
System.Collections.Generic.Dictionary drow = new System.Collections.Generic.Dictionary();
foreach (DataColumn dc in dtb.Columns)
{
drow.Add(dc.ColumnName, dr[dc.ColumnName]);
}
dic.Add(drow);

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