分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

ServiceStack DateTime数据类型转Json出现的困扰

发布时间:2023-09-06 02:35责任编辑:彭小芳关键词:数据类型
原文: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(0); ???????????}); ???????} ???}

打开Postman再次运行,查看结果

前段js再次取得该字符串时间,就可以正确的识别时间类型字段了。

ServiceStack DateTime数据类型转Json出现的困扰

原文地址:https://www.cnblogs.com/lonelyxmas/p/10570550.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved