ServiceStack.Text json中序列化日期格式问题的解决
在使用ServiceStack.Text的序列化为json格式的时候,当属性为datetime的时候,返回的是一个new date(324234234)的字符串,看着非常不爽。 如果是js来获取结果还好,
如果是c#获取这种字符串是没有办法转化为时间的。 所以我改造了下,让返回的是‘2015-06-06 09:11:11’的格式。
先获取源码, 然后在jsconfig.cs中加入如下代码
- private static bool dMsDatetimeFormat=true;
- public static bool MsDatetimeFormat
- {
- get { return dMsDatetimeFormat; }
- set { dMsDatetimeFormat = value; }
- }
这是用来标示是否转化为刚才看到的那种自定义的格式。
接着 修改DateTimeSerializer.cs文件的 public static void WriteWcfJsonDate(TextWriter writer, DateTime dateTime)方法
修改后的代码如下
- public static void WriteWcfJsonDate(TextWriter writer, DateTime dateTime)
- {
- if (JsConfig.AssumeUtc && dateTime.Kind == DateTimeKind.Unspecified)
- {
- dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
- }
- if (JsConfig.DateHandler == DateHandler.ISO8601)
- {
- writer.Write(dateTime.ToString("o", CultureInfo.InvariantCulture));
- return;
- }
- if (JsConfig.DateHandler == DateHandler.RFC1123)
- {
- writer.Write(dateTime.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture));
- return;
- }
- var timestamp = dateTime.ToUnixTimeMs();
- string offset = null;
- if (dateTime.Kind != DateTimeKind.Utc)
- {
- if (JsConfig.DateHandler == DateHandler.TimestampOffset && dateTime.Kind == DateTimeKind.Unspecified)
- offset = UnspecifiedOffset;
- else
- offset = LocalTimeZone.GetUtcOffset(dateTime).ToTimeOffsetString();
- }
- else
- {
- // Normally the JsonDateHandler.TimestampOffset doesn't append an offset for Utc dates, but if
- // the JsConfig.AppendUtcOffset is set then we will
- if (JsConfig.DateHandler == DateHandler.TimestampOffset && JsConfig.AppendUtcOffset.HasValue && JsConfig.AppendUtcOffset.Value)
- offset = UtcOffset;
- }
- if (JsConfig.MsDatetimeFormat)
- {
- writer.Write(dateTime.ToString("yyyy-MM-dd hh:mm:ss"));
- }
- else
- {
- writer.Write(EscapedWcfJsonPrefix);
- writer.Write(timestamp);
- if (offset != null)
- {
- writer.Write(offset);
- }
- writer.Write(EscapedWcfJsonSuffix);
- }
- }
这样再使用序列化方法的时候返回的就是’2015-09-08 11:11:11’的格式了
- ServiceStack.Text.JsonSerializer.SerializeToString(new
- { date=DateTime.Now,
- SecureContent = "here's some secure content that you can only see if you provide a correct apiKey",
- User = "user"
- })
当你不想用的时候,想恢复默认的方式,只要序列化前用这句就行了
ServiceStack.Text.JsConfig.MsDatetimeFormat = false;
ServiceStack.Text json中序列化日期格式问题的解决的更多相关文章
- springMvc返回Json中自定义日期格式
(一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...
- json中的日期格式转换(扩展new date()显示格式)
在java spring mvc 开发过程中,通过json 格式,向前端传递数据,日期格式发生了转变, 在前台数据展示时,要进行一定格式的转换才能正常显示: 我在开发中使用了easy ui 和my ...
- (十七)springMvc 对表单提交的日期以及JSON中的日期的参数绑定
文章目录 前言 `Ajax`提交表单数据 `Ajax`提交`JSON` 格式数据 解决输出JSON乱码的问题 控制JSON输出日期格式 小记 前言 springMVC 提供强大的参数绑定功能,使得我们 ...
- spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- [.Net Core 3.0+/.Net 5] System.Text.Json中时间格式化
简介 .Net Core 3.0开始全新推出了一个名为System.Text.Json的Json解析库,用于序列化和反序列化Json,此库的设计是为了取代Json.Net(Newtonsoft.Jso ...
- SpringMVC返回Json,自定义Json中Date类型格式
http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...
- 在Gridview 中 对日期格式的控制
在数据库中保存日期格式的时候,我们需要在客户端的显示有自己的要求 这就需要对 datatime 类型的数据进行控制,使之显示为你需要的格式 数据库中 如果不对其进行控制,显示的格式为 当在前端页面上进 ...
- Java处理Excel中的日期格式
Java处理Excel中的日期格式 2011-12-23 17:34:03| 分类: java |举报 |字号 订阅 下载LOFTER 我的照片书 | 在Excel中的日期格式,其数值为距离1 ...
- 工具类:关于解决数据库中的日期格式,经过response.getWriter().write(json)打到前台日期格式混乱的问题的总结
经过response.getWriter().write(json)打到前台日期格式混乱的问题的总结 import java.text.SimpleDateFormat;import net.sf.j ...
随机推荐
- VBA7种文档遍历法
Sub 在选定文档最后加入一句话() '遍历文件 Dim MyDialog As FileDialog On Error Resume Next Application.ScreenUpdating ...
- Python基础之爬虫(持续更新中)
python通过urllib.request.urlopen("https://www.baidu.com")访问网页 实战,去网站上下载一只猫的图片 import urllib. ...
- Bootstrap-Plugin:提示工具(Tooltip)插件
ylbtech-Bootstrap-Plugin:提示工具(Tooltip)插件 1.返回顶部 1. Bootstrap 提示工具(Tooltip)插件 当您想要描述一个链接的时候,提示工具(Tool ...
- 国外接活网站Elance, Freelancer和ScriptLance的介绍和对比
国外接活网站Elance, Freelancer和ScriptLance的介绍和对比littleben 一年以前 (via WEB)http://www.geekpark.net/entity/vie ...
- 配置myslq提示 the configuration step starting server is taking longer than expected we apologize for thi
.卸载重新安装,勾选日志配置选项,自定义日志输出路径
- django-redis缓存
1.安装django依赖包 pip install djange-redis==4.8.0 2.配置文件settings 需要开启redis服务 sudo service redis start,否 ...
- 使用Fiddler对IPhone手机的应用数据进行抓包分析
原文出自: http://www.cr173.com/html/20064_1.html Fiddler能捕获ISO设备发出的请求,比如IPhone, IPad, MacBook. 等等苹果的设备. ...
- unicat,multicast,broadcast区别
单播.多播和广播单播”(Unicast).“多播”(Multicast)和“广播”(Broadcast)这三个术语都是用来描述网络节点之间通讯方式的术语.那么这些术语究竟是什么意思?区别何在? 1.单 ...
- volyaire重振Infiniband
InfiniBand简 称IB,DoSTOR存储小字典里的解释是,一种新的I/O总线技术,用于取代目前的PCI总线.IB主要应用在企业网络和数据中心,也可以应用在高速线 速路由器.交换机.大型电信设备 ...
- css常用属性初总结:font
平时在做项目时,UX常说的一句话就是“这里的字体不对吧,字体大小也不太对,你们前端有没有按规范来”,今天,我们就一起来看看这折磨人的font属性. 字体属性font-family 允许值 系列名 初始 ...