1 /// <summary>
2 /// 将datatable转换为json
3 /// </summary>
4 /// <param name="dtb">Dt</param>
5 /// <returns>JSON字符串</returns>
6 public static string Dtb2Json(DataTable dtb)
7 {
8 JavaScriptSerializer jss = new JavaScriptSerializer();
9 System.Collections.ArrayList dic = new System.Collections.ArrayList();
10 foreach (DataRow dr in dtb.Rows)
11 {
12 System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
13 foreach (DataColumn dc in dtb.Columns)
14 {
15 drow.Add(dc.ColumnName, dr[dc.ColumnName]);
16 }
17 dic.Add(drow);
18
19 }
20 //序列化
21 return jss.Serialize(dic);
22 }
请发表评论