自定义JsonResult处理JSON序列化DateTime类型数据(Ext4.2+ASP.NET MVC 4)

最近项目中前台页面使用Extjs4.2 ,在后台ASP.NET MVC4返回的DateTime类型的数据错返回的DateTime类型的JsonResult的结果中的值是“\/Date(1378446180000)\/此种格式,导致页面显示时间不正确(如下图创建时间列)

 

 

于是通过自定义JsonResult处理了JSON序列化DateTime类型数据,将处理方法贴于此,供需要的朋友交流学习!!!!

创建CustomResult类,继承JsonResult, 重写 ExecuteResult方法,代码如下:

 public class CustomResult : JsonResult
    {


        public override void ExecuteResult(ControllerContext context) 
        {

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
           
           
            HttpResponseBase response = context.HttpContext.Response;
            if (!string.IsNullOrEmpty(this.ContentType))
            {
                response.ContentType = this.ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            if (this.ContentEncoding != null)
            {
                response.ContentEncoding = this.ContentEncoding;
            }
            if (this.Data != null)
            {
                IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); 
                timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; 
                response.Write(JsonConvert.SerializeObject(this.Data,    Newtonsoft.Json.Formatting.Indented, timeFormat));
                
             
              
            } 
        
        
        }
    
    }

  创建BaseController类,继承Controller类,重写Json方法

 public class BaseController : Controller
    {

        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding)
        {


            return new CustomResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };
       
        }

    }

  这样就OK了(对了还要解释下,在CustomResult类里用到的序列化是Newtonsoft.Json.Converters),之后针对处理返回带时间格式的 项目中XXController直接继承BaseController 就可以了

处理完的表格的结果如下

 

 

转载于:https://www.cnblogs.com/Y-X-DONG/p/3336597.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值