【发布时间】:2016-07-25 13:53:07
【问题描述】:
我正在使用 Json.NET/newtonsoft,并且我有以下 C# 类:
public class EntityDefinition
{
[DataMember]
public string CreatedBy { get; set; }
[DataMember]
[JsonProperty(ItemConverterType = typeof(IsoDateTimeConverter))]
public DateTime CreatedOn { get; set; }
}
当我尝试在我的 wcf 中返回此类时,我得到以下 JSON:
{
"GetDefinitionResult": {
"CreatedBy": "Dor",
"CreatedOn": "/Date(1466428742000+0300)/"
}
}
如何在没有“Date(”的情况下获取要解析的日期,仅表示毫秒或 iso 格式“yyy-mm-dd”
我尝试使用 JsonProperty 转换器,但它仍然返回“Date()”
[JsonProperty(ItemConverterType = typeof(IsoDateTimeConverter))]
【问题讨论】:
-
JsonPropertyAttribute.ItemConverterType是用于collection items的转换器。你想要JsonConverter。但是你为什么要标记这个wcf? WCF 不使用 Json.NET,它 usesDataContractSerializer. -
您可以为您的问题创建一个minimal reproducible example 吗? Json.NET 默认不输出该格式的日期,所以我想你可能在不知不觉中使用了
DataContractSerializer。 -
Json.NET not 返回该格式。默认情况下,它返回 ISO 格式。您不需要为此使用任何属性。另一方面,WCF 不使用 Json.NET,它使用非常非常古老的 DataContractSerializer。
标签: c# json wcf datetime json.net