C# DataTable转json 时间格式化

1.NewTonSoft.json

C# DataTable转json 时间格式化
 public static string DataTableToJson(DataTable dt)
        {
            if (dt == null && dt.Rows.Count <= 0)
            {
                return "";
            }
            else
            {
                IsoDateTimeConverter convert = new IsoDateTimeConverter();
                convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                return JsonConvert.SerializeObject(dt, Formatting.None, convert);

            }
              
        }
View Code

2.net frework 版本4.5以上

C# DataTable转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<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
                foreach (DataColumn dc in dtb.Columns)
                {
                    drow.Add(dc.ColumnName, dr[dc.ColumnName]);
                }
                dic.Add(drow);

            }
            //序列化  
            string jsonStr = jss.Serialize(dic);
            jsonStr = System.Text.RegularExpressions.Regex.Replace(jsonStr, @"\\/Date\((\d+)\)\\/", match =>
            {
                DateTime dt = new DateTime(1970, 1, 1);
                dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                dt = dt.ToLocalTime();
                return dt.ToString("yyyy-MM-dd HH:mm:ss");
            });
            return jsonStr;
        }
View Code

 

C# DataTable转json 时间格式化

上一篇:WPF 实现 DataGrid/ListView 分页控件(转)


下一篇:Delphi 与 XML 示例(直接利用IXMLDocument)