简体   繁体   中英

Json.NET Serializing / Deserializing Array of Datetimes with Custom Format

I am trying to apply a custom datetime format to an array of datetime values. I know you can use IsoDateTimeConverter to accomplish this for single values eg

class CustomDateTimeConverter : IsoDateTimeConverter
{
    public CustomDateTimeConverter()
    {
        base.DateTimeFormat = "yyyy-MM-dd";
    }
}

class ReturnObjectA 
{
    [JsonConverter(typeof(CustomDateTimeConverter))]
    public DateTime ReturnDate { get; set;}
}

But how do apply the same thing to an array of datetimes? eg The following does not work.

class ReturnObjectA 
{
    [JsonConverter(typeof(CustomDateTimeConverter))]
    public DateTime[] ReturnDate { get; set;}
}

JsonProperty is what you're looking for:

class ReturnObjectA
{
    [JsonProperty(ItemConverterType = typeof(CustomDateTimeConverter))]
    public DateTime[] ReturnDate { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM