1

Here is a similar issue, but it didn't get any answers. How to format a list of dates using JsonConverter attribute

Here is the question: I try to use a JsonConverter attribute using a custom IsoDateTimeConverter on the DateTime fields that need to be formatted as ISO date time format

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


public class A
{

    [JsonConverter(typeof(DateConverter))]
    public List<DateTime> Example { get; set; }
}

This results in the following error:

Unexpected value when converting date. Expected DateTime or DateTimeOffset, got System.Collections.Generic.List`1[System.DateTime].

I checked all the documents from https://www.newtonsoft.com, but I can't find anything relate to this issue.

Please give me some ideas about that. Thanks

dbc
  • 104,963
  • 20
  • 228
  • 340
Orange
  • 31
  • 4
  • 1
    Found better answer here ; https://stackoverflow.com/questions/36580519/custom-json-serialization-for-each-item-in-ienumerable?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Orange May 09 '18 at 19:03

1 Answers1

0

How about trying something like this since it requires to pass in a DateTime Object (Let A be the single converted value and B be the container of the list of DateTimes):

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


public class A
{

    [JsonConverter(typeof(DateConverter))]
    public DateTime date { get; set; }
}

public class B
{
    public List<A> dates { get; set; }
}
user1321988
  • 513
  • 2
  • 6
  • https://stackoverflow.com/questions/36580519/custom-json-serialization-for-each-item-in-ienumerable?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa Here is the better answer – Orange May 09 '18 at 19:04