C# DataTable转json 时间格式化

1.NewTonSoft.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);

            }

        }

2.net frework 版本4.5以上

        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;
        }

原文地址:https://www.cnblogs.com/SoftWareIe/p/11067675.html

时间: 2024-08-01 08:41:25

C# DataTable转json 时间格式化的相关文章

Newtonsoft.Json 时间格式化问题

1.WPF加载时进行全局设置 Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings(); JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() => { setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.M

json时间格式化问题

1 function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 2 try { 3 var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10)); 4 var month = date.getMonth() + 1 < 10 ? "0" + (date

json时间格式化

//格式化日期字符串 String.prototype.jsonDateFormat = function (format) { var date, timestamp, dtObj timestamp = parseInt(this.toString().replace('/Date(', '').replace(')/', '').replace(/\+\d+/, ''), 10)// jsonDt.replace(/\/Date\((\d+)\)\//, "$1"); date

JSON时间转换格式化(将数字时间转换为可视化的yyyy-MM-dd HH:mm:ss的格式)

参考: http://blog.csdn.net/u012992462/article/details/47042535 http://www.cnblogs.com/kissdodog/p/5419923.html 内容: 通常JSON时间一般是这样的格式. /Date(1436595149269)/ 通常我们用AJAX获取下来的JSON数据,如果有时间,都是这种格式的.其中,中间的一段数字"1436595149269"表示的是1970年1月1日至今的毫秒数. 这种时间格式并不能够直

JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List&lt;T&gt;转JSON,JSON转List&lt;T&gt;,JSON转C#对象

一.JSON解析与字符串化                                                                                            JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 filter,指定要序列化的属性名 indent,格式化为可读的代码,可指定分隔符或指定的缩进空格个数 var man

Datatable转换为Json 然后把Json数据放入 js 文件中

C#中把Datatable转换为Json的5个代码实例 /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table">Datatable对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable dt) { StringBuilde

JSon 事件格式化

JS~json日期格式化 起因 对于从C#返回的日期字段,当进行JSON序列化后,在前台JS里显示的并不是真正的日期,需要格式化时间 实现 function ChangeDateFormat(jsondate) { jsondate = jsondate.replace("/Date(", "").replace(")/", ""); if (jsondate.indexOf("+") > 0) {

C#中把Datatable转换为Json的5个代码实例

一. /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table">Datatable对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable dt) { StringBuilder jsonString = new String

EasyUI的DataGrid日期列(datebox)正确显示json时间格式

问题描述: 前端使用EasyUI,后台使用Spring MVC, 数据库里面存储的时间格式为:2014-06-10,但是后台返回给前台页面的数据是json格式的,类似于:1402367297000的形式,日期列datebox是无法解析的.具体如下图: 自己也是EasyUI小白,网上查查资料,倒腾下总算搞出来了,这里做下记录. 一般情况下我们所需的日期格式都是:2014-02-02或者2014/09/09形式的,因此首先要考虑实现一个添加日期格式化的插件. jQuery日期格式化 在自己的js中添