当前位置: 移动技术网 > IT编程>开发语言>.net > ServiceStack DateTime数据类型转Json出现的困扰

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

2019年03月22日  | 移动技术网IT编程  | 我要评论

win8 ghost,建湖新闻,顺藤摸瓜的近义词

执行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再次取得该字符串时间,就可以正确的识别时间类型字段了。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网