最近项目中前台页面使用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. Qt的Script、Quick、QML的关系与总结

    背景 最近在学QML,感觉也不难,就是一直以来接触 Qt 的脚本类的东西的顺序是Script.Quick1.Declarative.Quick2.QML.那么每一个都是干什么的呢,这些东西搞的我有点混 ...

  2. 关于NSIS脚本的Demo

    这个NSIS脚本是打包公司的整个Release项目工程. 用NSIS编译器编译下就可以了. ; Script generated by the HM NIS Edit Script Wizard. ! ...

  3. VS2010使用Qt库

    有参考文件可以看出,应该还是简单的. = =! Qt库的安装目录bin下,我把它添加到Path环境变量中也无用,真是搞不懂,按理来说windows搜索完当前目录会去Path指定的路径下搜索啊?为什么必 ...

  4. 什么是空间复杂度(What is actually Space Complexity ?)

    属于空间复杂度(Space Complexity)在很多情况下被错认为是附属空间(Auxiliary Space),下面是附属空间和空间复杂度的定义. 附属空间(Auxiliary Space)是算法 ...

  5. c语言二维数组变色龙之死字的打印

    1 #include <stdio.h> #include <stdlib.h> void main() { ][]= { {'#','#','#',' ','#','#',' ...

  6. 第19讲- UI组件之_Button、checkbox、radio

    第19讲 UI组件之_Button.checkbox.radio 四.按钮Button Button继承自TextView,间接继承自View.当用户对按钮进行操作的时候,触发相应事件,如点击,触摸. ...

  7. qt tablewidget中单个和批量删除代码如下(部分)截图如下

    def coltable(self):#行删除    row=self.downwidget.currentRow()    select=self.downwidget.isItemSelected ...

  8. python使用一个集合代替列表

    """说明:对于一个指定的序列,如果需要获得一个只包含该序列中不重复的序列时,使用以下算法:"""seq=['a','a','b','c', ...

  9. java foreach循环为什么不能赋值

    直接上代码 public class test4 { public static void main(String args[]){ int [] a=new int[3]; for(int j:a) ...

  10. bit、byte、位、字节、字符串等概念

    原始文章:http://djt.qq.com/article/view/658 1.古代送信:马车,烽火,信鸽 2.1837年,世界第一条电报诞生, 美国科学家莫尔斯尝试用一些“点”和“划”来表示不同 ...