最近项目中前台页面使用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 就可以了

处理完的表格的结果如下

自定义JsonResult处理JSON序列化DateTime类型数据(Ext4.2+ASP.NET MVC 4)的更多相关文章

  1. json序列化datetime类型数据

    错误描述: import jsonimport datetime a = datetime.datetime.now()print(a) b = json.dumps(a)print(b) 如上代码, ...

  2. 解决python中转化成json的方法不能序列化datetime类型数据(转)

    Python自带的json.dumps方法序列化数据时候如果格式化的数据中有datetime类型数据时候会提示错误TypeError: datetime.datetime(2012, 12, 12, ...

  3. Python—使用Json序列化Datetime类型

    import json from datetime import datetime, date """ str,int,list,tuple,dict,bool,None ...

  4. 使用Newtonsoft.json 解决 Asp.Net MVC DateTime类型数据Json格式化问题

    解决思路 众所周知,MVC中调用的微软的组件JavaScriptSerialer...,格式DateTime类型数据需要在客户端专门解. 还知道,NewtonSoft.json可以“正确”的格式化Da ...

  5. Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案

    使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题). 若要转换成DateTime没有T,可以加上特性: pub ...

  6. 【时区问题】SpringBoot+mybatis查询mysql的datetime类型数据时间差14小时

    [时区问题]MyBatis查询MySQL的datetime类型数据时间差14小时 故障解决方式 与数据库连接时,定义时区,避免mybatis框架从mysql获取时区.在连接上加上 serverTime ...

  7. 用js解析经json序列化后的C#的DateTime类型数据

    格式化日期(网上到处是),把下面的代码添加到jQuery.js文件中 //格式化日期 Date.prototype.format = function(format) { /* * eg:format ...

  8. [python] 查询mysql返回datetime类型数据的处理

    Python 查询Mysql,如果是datetime类型,在json序列化的时候会出现问题. 在网上查了一下,解决方案基本都是遍历dict数据,如果是datetime则转化为字符串. from dat ...

  9. .net core json序列化 long类型转化成字符串

    实现类 using System; using System.ComponentModel; using System.Linq; using Newtonsoft.Json; namespace H ...

随机推荐

  1. js深入研究之扩展类,克隆对象,混合类(自定义的extend函数,clone函数,与augment函数)

    1.类扩展 /* EditInPlaceField类 */ /* 扩展函数 */ function extend(subClass, superClass) { var F = function() ...

  2. C(m,n)%P

    program1 n!%P(P为质数) 我们发现n! mod P的计算过程是以P为周期的的,举例如下: n = 10, P = 3 n! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 ...

  3. usaco5.5-Hidden Passwords

    最小表示法,感觉可以做成个模板,第一次RE是因为字符串长度变2倍了而我把数组开小了 Executing...   Test 1: TEST OK [0.008 secs, 3760 KB]   Tes ...

  4. DL,DT,DD,比传统table更语义,解析更快的table列表方式

    使用dl,dt,dd替代传统的table布局 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  5. 用Update Select批量更新某一字段的值[可以跨库]

    SQL:UPDATE test1  SET name = (SELECT y.name  FROM  DB2.dbo.test2 y WHERE test1.id = y.id)

  6. c语言通过时间种子产生随机数并选出最大值以及下标

    1 #include <stdio.h> #include <stdlib.h> #include <time.h> //2016 10 10 void main( ...

  7. SQL 使用经验

    1. 写存储过程,Update数据库表,一定要根据idRow,也就是主键唯一键来更新. 更新操作,如果根据其他条件更新,之后就有的忙活了. 2. Query语句要加NOLOCK

  8. JSF标签大全详解

    1. JSF入门 藉由以下的几个主题,可以大致了解JSF的轮廓与特性,我们来看看网页设计人员与应用程序设计人员各负责什么. 1.1简介JSF Web应用程序的开发与传统的单机程序开发在本质上存在着太多 ...

  9. gcc/g++/make 编译信息带颜色输出

    假设编译一个项目错误警告太多.很不好找,所以很希望输出信息能够带有颜色. 但是 gcc 4.9.0 之前的版本号并不支持,非常多情况下是不能替换编译器的,比方使用交叉编译器, 也能够使用 colorg ...

  10. JSP实现分页功能

    分页须知知识点: (1)JDBC2.0的可滚动结果集. (2)HTTP GET请求. 一.可滚动结果集   Connection con  = DriverManager.getConnection( ...