如何在NancyFX中将DateTimeOffset序列化为JSON? [英] How to serialize DateTimeOffset as JSON in NancyFX?

查看:110
本文介绍了如何在NancyFX中将DateTimeOffset序列化为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用默认的JSON序列化程序从我的Nancy应用程序返回一些JSON.我有以下DTO课:

I'm attempting to return some JSON from my Nancy application, using the default JSON serializer. I've got the following DTO class:

class Event
{
    public DateTimeOffset Timestamp { get; set; }
    public string Message { get; set; }
}

当我将其退回时,如下所示:

When I return it, as follows:

return Response.AsJson(
    new Event { Message = "Hello", Timestamp = DateTime.UtcNow });

...我得到了所有返回的DateTimeOffset属性,所以看起来像这样:

...I get all of the DateTimeOffset properties returned, so it looks like this:

"Timestamp": {
    "DateTime":"\/Date(1372854863408+0100)\/",
    "UtcDateTime":"\/Date(1372858463408)\/",
    "LocalDateTime":"\/Date(1372858463408+0100)\/",
    "Date":"\/Date(1372806000000+0100)\/",
    "Day":3,
    "DayOfWeek":3

我期待的是"Timestamp":"\/Date(1372854863408+0100)\/",没有其他东西.这是Nancy用于DateTime值的格式.

I was expecting "Timestamp":"\/Date(1372854863408+0100)\/", with none of the other stuff. This is the format that Nancy uses for DateTime values.

如何配置Nancy以相同的样式输出DateTimeOffset值?

How do I configure Nancy to output DateTimeOffset values in the same style?

推荐答案

我相信它是您为什么不能使用这种方法?

Any reason you can't use this approach?

return Response.AsJson(
    new Event { Message = "Hello", Timestamp = DateTime.UtcNow.ToString() });

这篇关于如何在NancyFX中将DateTimeOffset序列化为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆