问题描述
我正在尝试使用默认的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?
推荐答案
我相信它是内置的JsonSerializer 对此负责.
您为什么不能使用这种方法?
Any reason you can't use this approach?
return Response.AsJson(
new Event { Message = "Hello", Timestamp = DateTime.UtcNow.ToString() });
这篇关于如何在NancyFX中将DateTimeOffset序列化为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!