C# Newtonsoft.Json.Formatting DateTime 日期格式化
List<Record> list = new List<Record>(); for (int i = 0; i < 10; i++) { Record item = new Record(); item.Name = "A" + i; item.CreateTime = DateTime.Now; list.Add(item); Thread.Sleep(2000); } var noformatJson = JsonConvert.SerializeObject(list); log.Debug($"没格式化 => {noformatJson}"); IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; //对日期进行格式化 var jsonSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Converters = { timeConverter } }; var formatJson = JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.Indented, jsonSettings); log.Debug($"格式化后 => {formatJson}");