1 /// <summary> 2 /// 将datatable转换为json 3 /// </summary> 4 /// <param name="dtb">Dt</param> 5 /// <returns>JSON字符串</returns> 6 public 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 string jsonStr= jss.Serialize(dic); 22 jsonStr = System.Text.RegularExpressions.Regex.Replace(jsonStr, @"\\/Date\((\d+)\)\\/", match => 23 { 24 DateTime dt = new DateTime(1970, 1, 1); 25 dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value)); 26 dt = dt.ToLocalTime(); 27 return dt.ToString("yyyy-MM-dd HH:mm:ss"); 28 }); 29 return jsonStr; 30 }