public class JsonDateTimeConverter : JsonConverter<DateTime?> { private readonly string _dateTimeFormat; public JsonDateTimeConverter(string dateTimeFormat) { _dateTimeFormat = dateTimeFormat; } public override void WriteJson(JsonWriter writer, DateTime? value, JsonSerializer serializer) { switch (value) { case null: return; case { } dateTime: writer.WriteValue(dateTime.ToString(_dateTimeFormat)); break; } } public override DateTime? ReadJson(JsonReader reader, Type objectType, DateTime? existingValue, bool hasExistingValue, JsonSerializer serializer) { if (reader.Value == null) return null; return DateTime.Parse(reader.Value.ToString()); } }
[JsonProperty("day"), JsonConverter(typeof(JsonDateTimeConverter), "yyyy-MM-dd HH:mm:ss")] public DateTime? Day { get; set; }
标签:JsonConverter,C#,JsonDateTimeConverter,DateTime,Json,dateTimeFormat,null,public From: https://www.cnblogs.com/log9527blog/p/18460168