在使用ServiceStack.Text的序列化为json格式的时候,当属性为datetime的时候,返回的是一个new date(324234234)的字符串,看着非常不爽。 如果是js来获取结果还好,

如果是c#获取这种字符串是没有办法转化为时间的。 所以我改造了下,让返回的是‘2015-06-06 09:11:11’的格式。

先获取源码, 然后在jsconfig.cs中加入如下代码

Code Snippet
  1. private static bool dMsDatetimeFormat=true;
  2.       public static bool MsDatetimeFormat
  3.       {
  4.           get { return dMsDatetimeFormat; }
  5.           set { dMsDatetimeFormat = value; }
  6.       }

这是用来标示是否转化为刚才看到的那种自定义的格式。

接着 修改DateTimeSerializer.cs文件的   public static void WriteWcfJsonDate(TextWriter writer, DateTime dateTime)方法

修改后的代码如下

Code Snippet
  1. public static void WriteWcfJsonDate(TextWriter writer, DateTime dateTime)
  2.     {
  3.         if (JsConfig.AssumeUtc && dateTime.Kind == DateTimeKind.Unspecified)
  4.         {
  5.             dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
  6.         }
  7.         if (JsConfig.DateHandler == DateHandler.ISO8601)
  8.         {
  9.             writer.Write(dateTime.ToString("o", CultureInfo.InvariantCulture));
  10.             return;
  11.         }
  12.         if (JsConfig.DateHandler == DateHandler.RFC1123)
  13.         {
  14.             writer.Write(dateTime.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture));
  15.             return;
  16.         }
  17.         var timestamp = dateTime.ToUnixTimeMs();
  18.         string offset = null;
  19.         if (dateTime.Kind != DateTimeKind.Utc)
  20.         {
  21.             if (JsConfig.DateHandler == DateHandler.TimestampOffset && dateTime.Kind == DateTimeKind.Unspecified)
  22.                 offset = UnspecifiedOffset;
  23.             else
  24.                 offset = LocalTimeZone.GetUtcOffset(dateTime).ToTimeOffsetString();
  25.         }
  26.         else
  27.         {
  28.             // Normally the JsonDateHandler.TimestampOffset doesn't append an offset for Utc dates, but if
  29.             // the JsConfig.AppendUtcOffset is set then we will
  30.             if (JsConfig.DateHandler == DateHandler.TimestampOffset && JsConfig.AppendUtcOffset.HasValue && JsConfig.AppendUtcOffset.Value)
  31.                 offset = UtcOffset;
  32.         }
  33.         if (JsConfig.MsDatetimeFormat)
  34.         {
  35.             writer.Write(dateTime.ToString("yyyy-MM-dd hh:mm:ss"));
  36.         }
  37.         else
  38.         {
  39.             writer.Write(EscapedWcfJsonPrefix);
  40.             writer.Write(timestamp);
  41.             if (offset != null)
  42.             {
  43.                 writer.Write(offset);
  44.             }
  45.             writer.Write(EscapedWcfJsonSuffix);
  46.         }
  47.     }

这样再使用序列化方法的时候返回的就是’2015-09-08  11:11:11’的格式了

Code Snippet
  1. ServiceStack.Text.JsonSerializer.SerializeToString(new
  2.                 {   date=DateTime.Now,
  3.                     SecureContent = "here's some secure content that you can only see if you provide a correct apiKey",
  4.                     User = "user"
  5.                 })

当你不想用的时候,想恢复默认的方式,只要序列化前用这句就行了

ServiceStack.Text.JsConfig.MsDatetimeFormat = false;

ServiceStack.Text json中序列化日期格式问题的解决的更多相关文章

  1. springMvc返回Json中自定义日期格式

    (一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...

  2. json中的日期格式转换(扩展new date()显示格式)

    在java  spring mvc 开发过程中,通过json 格式,向前端传递数据,日期格式发生了转变, 在前台数据展示时,要进行一定格式的转换才能正常显示: 我在开发中使用了easy ui 和my ...

  3. (十七)springMvc 对表单提交的日期以及JSON中的日期的参数绑定

    文章目录 前言 `Ajax`提交表单数据 `Ajax`提交`JSON` 格式数据 解决输出JSON乱码的问题 控制JSON输出日期格式 小记 前言 springMVC 提供强大的参数绑定功能,使得我们 ...

  4. spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  5. [.Net Core 3.0+/.Net 5] System.Text.Json中时间格式化

    简介 .Net Core 3.0开始全新推出了一个名为System.Text.Json的Json解析库,用于序列化和反序列化Json,此库的设计是为了取代Json.Net(Newtonsoft.Jso ...

  6. SpringMVC返回Json,自定义Json中Date类型格式

    http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...

  7. 在Gridview 中 对日期格式的控制

    在数据库中保存日期格式的时候,我们需要在客户端的显示有自己的要求 这就需要对 datatime 类型的数据进行控制,使之显示为你需要的格式 数据库中 如果不对其进行控制,显示的格式为 当在前端页面上进 ...

  8. Java处理Excel中的日期格式

    Java处理Excel中的日期格式 2011-12-23 17:34:03|  分类: java |举报 |字号 订阅 下载LOFTER 我的照片书  |   在Excel中的日期格式,其数值为距离1 ...

  9. 工具类:关于解决数据库中的日期格式,经过response.getWriter().write(json)打到前台日期格式混乱的问题的总结

    经过response.getWriter().write(json)打到前台日期格式混乱的问题的总结 import java.text.SimpleDateFormat;import net.sf.j ...

随机推荐

  1. Jenkins集成selenium

    目的:将selenium用例集成到Jenkins,需要执行时,只需要执行curl命令即可. 1.准备selenium测试脚本 from selenium import webdriver import ...

  2. 七.jQuery源码解析之.toArray()

    toArray()是将jQuery对象转换成数组 从源码中可以看到,这些常见的方法,都是直接从原生的 javascript中"借鉴"过来的.为什么这么说呢? 225行中,在运行时, ...

  3. 好久不见(致win7)

    7月8号,电脑上装了pgp,然后说让重启,重启之后蓝屏,自此,就一直蓝屏了 电脑装了双系统,工作时用centos,我不愿重装系统,怕centos受影响 网上说安装模式下可以卸载软件,可我在安全模式下, ...

  4. 使用word设置标题级别, 自动生成和大纲对应的多级列表, 自动生成索引目录

    作为程序员,只会开发是不够的, 在日常工作中还需要掌握一些办公软件的的操作方法,word excel ppt精通不敢, 暂且入个门吧, 在前后台开发配合过程中,能写的一手好文档将会达到事半功倍的效果, ...

  5. python写exploit采集器

    前言: 根据天文地理与风水学,我掐指一算的看到了一篇不错的文章,文章里面写到整理exploit 我顿时心理想写一个exploit采集器,那么说时迟那时快.就开始写了 代码: 思路:http://exp ...

  6. Creating Self-Signed SSL Certificates

    http://weblogic-wonders.com/weblogic/2011/05/25/ssl-configuration-for-weblogic-server/ http://m-butt ...

  7. Spring 3.1 entityManagerFactory java.lang.NoSuchFieldError: NULL Error

    This means there is a version mismatch--most likely with spring classes. So make sure all your sprin ...

  8. 光圈、曝光、ISO

    光圈大小对景深的影响: 光圈大小示意图(值越小光圈越大) 光圈.曝光.ISO对图像效果影响

  9. spring学习---day01

    1,Spring Boot项目在启动的时,修改默认图标: 在src/main/resources目录下新建banner.txt文件,然后将自己的图案黏贴进去即可.ASCII图案可通过网站http:// ...

  10. EmEditor的正则表达式

    前提是 "使用正则表达式"的复选框打上勾. 1 查找<>之间的字符串:   ".*?"2 查找双引号之间的字符串:   ".*?" ...