在ASP.NET Web API的JSON输出中没有时间的日期 [英] Dates without time in ASP.NET Web API's JSON output
本文介绍了在ASP.NET Web API的JSON输出中没有时间的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的方法来配置JSON.NET,以便某些 DateTime
字段将被格式化,无需时间和其他 DateTime
字段仍然会被格式化为时间?
示例:
{firstName:'John',lastName:'Doe',birthday:'1965-09-23'}
解决方案
如果您只需要影响特定字段,请先创建一个转换器类型:
public class OnlyDateConverter:IsoDateTimeConverter
{
public OnlyDateConverter()
{
DateTimeFormat =yyyy-MM-dd;
}
}
然后将此属性添加到任何字段/属性想要这样:
[JsonConverter(typeof(OnlyDateConverter))]
pre>
Is there a simple way to configure JSON.NET so that some
DateTime
fields will be formatted without time and otherDateTime
fields will still be formatted with time?Example:
{ firstName: 'John', lastName : 'Doe', birthday: '1965-09-23' }
解决方案If you need this to only affect a particular field, create a converter type first:
public class OnlyDateConverter : IsoDateTimeConverter { public OnlyDateConverter() { DateTimeFormat = "yyyy-MM-dd"; } }
and then add this attribute to whatever fields/properties you want this for:
[JsonConverter(typeof(OnlyDateConverter))]
这篇关于在ASP.NET Web API的JSON输出中没有时间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文