ServiceStack DateTime数据类型转Json出现的困扰
执行dotnet-new selfhost sstest 创建项目,然后打开解决方案
修改ssTest.ServiceModel中的Hello.cs,在HellopResponse中添加时间属性,然后修改MyServices中的代码
运行程序,打开Postman查看返回结果
可以看到Json中date属性返回的是 "date": "/Date(1555810731732+0800)/",直接导致前段js无法识别该字段,该如何解决?
在Startup中加入以下代码,任何时间都转换为ISO8601字符串
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
JsConfig<DateTime>.SerializeFn = time => new DateTime(time.Ticks, DateTimeKind.Local).ToString("o");
JsConfig<DateTime?>.SerializeFn =
time => time != null ? new DateTime(time.Value.Ticks, DateTimeKind.Local).ToString("o") : null;
JsConfig.DateHandler = DateHandler.ISO8601; app.UseServiceStack(new AppHost()); app.Run(context =>
{
context.Response.Redirect("/metadata");
return Task.FromResult();
});
}
}
打开Postman再次运行,查看结果
前段js再次取得该字符串时间,就可以正确的识别时间类型字段了。
ServiceStack DateTime数据类型转Json出现的困扰的更多相关文章
- Python全栈--7模块--random os sys time datetime hashlib pickle json requests xml
模块分为三种: 自定义模块 内置模块 开源模块 一.安装第三方模块 # python 安装第三方模块 # 加入环境变量 : 右键计算机---属性---高级设置---环境变量---path--分号+py ...
- 11月13日上午ajax返回数据类型为JSON数据的处理
ajax返回数据类型为JSON数据的处理 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- sql server2000中使用convert来取得datetime数据类型样式(全)
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- Asp.Net WebAPI配置接口返回数据类型为Json格式
Asp.Net WebAPI配置接口返回数据类型为Json格式 一.默认情况下WebApi 对于没有指定请求数据类型类型的请求,返回数据类型为Xml格式 例如:从浏览器直接输入地址,或者默认的XM ...
- Pythoy 数据类型序列化——json&pickle 模块
Pythoy 数据类型序列化--json&pickle 模块 TOC 什么是序列化/反序列化 pickle 模块 json 模块 对比json和pickle json.tool 命令行接口 什 ...
- python datetime.datetime is not JSON serializable
1.主要是python list转换成json时对时间报错:datetime.datetime(2014, 5, 23, 9, 33, 3) is not JSON serializable. 2. ...
- datetime is not json serializable
python, datetime is not json serializable import datetime def json_serial(obj): """JS ...
- DateTime数据类型保存问题(DateTime2)
DateTime And DateTime2 问题: 从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值 原因: EF中model存在datetime类型的字段, ...
- SQL Server datetime数据类型设计、优化误区
一.场景 在SQL Server 2005中,有一个表TestDatetime,其中Dates这个字段的数据类型是datetime,如果你看到表的记录如下图所示,你最先想到的是什么呢? (图1:数据列 ...
随机推荐
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- Proxmox Reset Root Password
http://c-nergy.be/blog/?p=1777 Step 1 – Boot your Proxmox VE machine. In the boot menu screen, you s ...
- 初识jvm堆,栈参数
堆的分配参数: -Xmx //设立最大堆 -Xms //最小堆,初始化堆大小 -Xmn //设置新生代(eden+2*surviivor+old)大小 官方推荐:3/8Xmx------> ...
- SpringMVC 学习 八 SSM环境搭建(一) web.xml配置
第一步:导入jar包 注意包的兼容性,以后采用maven会好很多 第二步:配置web.xml 在web.xml中,主要的配置内容有以下几点 (1)spring容器配置文件的位置 <!-- spr ...
- Django的学习(二)————Templates
一.django的模板: 在settings.py的文件中可以看到并设置这个模板. 1.直接映射: 通过建立的文件夹(templates)和文件(html)来映射. <!DOCTYPE html ...
- 784. Letter Case Permutation
这个题的思想很重要,两种方法 第一种,回溯法 class Solution { public: int sz; vector<string> letterCasePermutation(s ...
- 2018.10.25 bzoj4565: [Haoi2016]字符合并(区间dp+状压)
传送门 当看到那个k≤8k\le 8k≤8的时候就知道需要状压了. 状态定义:f[i][j][k]f[i][j][k]f[i][j][k]表示区间[i,j][i,j][i,j]处理完之后的状态为kkk ...
- 走进JDK(三)------AbstractStringBuilder、StringBuffer、StringBuilder
AbstractStringBuilder是一个抽象类,StringBuffer.StringBuilder则继承AbstractStringBuilder,所以先说AbstractStringBui ...
- vue+mui轮播图
mui的轮播图,如果图片是请求来的,直接在html中循环是不会动的. 需要请求完图片之后,在setTimeout方法里,使用slider()方法,这样才会动 而且mui的轮播图,有点坑的,需要重复最后 ...
- nxn随机矩阵乘以概率向量依旧是概率向量
由上面可进一步推到出A*A是随机矩阵看成(A a1,A a2...A an) 所以A^m依然是随机矩阵.