首页 > Windows开发 > 详细

C#DataTable2Json(附时间格式化)

时间:2017-01-10 12:57:12      阅读:399      评论:0      收藏:0      [点我收藏+]
 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         }

 

C#DataTable2Json(附时间格式化)

原文:http://www.cnblogs.com/liudabao123/p/6268690.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!