简书链接:netcoreapijson时间序列化返回多种场景踩坑 文章字数:111,阅读全文大约需要1分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 builder.Services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new MyDateTimeConverter()); options.JsonSerializerOptions.Converters.Add(new DateTimeNullableConverter()); options.JsonSerializerOptions.AddContext<MyJsonContext>();//处理aot }); builder.Services.AddControllers().AddNewtonsoftJson(options =>//install-package Microsoft.AspNetCore.Mvc.NewtonsoftJson { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; // 设置时区为 UTC) options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; });
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 namespace webapi_mes.baseframework.convert { public class MyDateTimeConverter : JsonConverter<DateTime> { /* public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return DateTime.Parse(reader.GetString()); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss")); }*/ /// <summary> /// 获取或设置DateTime格式 /// <para>默认为: yyyy-MM-dd HH:mm:ss</para> /// </summary> /// public string DateTimeFormat { get; set; } = "yyyy-MM-dd HH:mm:ss"; public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)//=> DateTime.Parse(reader.GetString()); { //return DateTime.Parse(reader.GetString()); if (reader.TokenType == JsonTokenType.String) { if (DateTime.TryParse(reader.GetString(), out DateTime date)) return date; } return reader.GetDateTime(); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) //=> writer.WriteStringValue(value.ToString(this.DateTimeFormat)); { writer.WriteStringValue(value.ToString(this.DateTimeFormat)); } } public class DateTimeNullableConverter : JsonConverter<DateTime?> { public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return string.IsNullOrEmpty(reader.GetString()) ? default(DateTime?) : DateTime.Parse(reader.GetString()); } public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options) { writer.WriteStringValue(value?.ToString("yyyy-MM-dd HH:mm:ss")); } } }
参考链接https://learn.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support
但是上面的东西无法处理datatable里面的时间格式化
最后应该这样处理
1 2 3 4 5 JsonSerializerSettings settings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" }; JArray obj_row = JArray.FromObject(dataTable, JsonSerializer.Create(settings));//
也可以用如下方法
1 2 3 4 5 6 7 8 JsonSerializerSettings settings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" }; // Serialize the DataTable to a JSON string using the custom date format string string json = JsonConvert.SerializeObject(table, settings);
下面是测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 using System; using System.Data; using Newtonsoft.Json; // Create a DataTable with a DateTime column DataTable table = new DataTable(); table.Columns.Add("Date", typeof(DateTime)); // Add some rows to the table table.Rows.Add(DateTime.Now); table.Rows.Add(DateTime.Now.AddDays(1)); // Create a serializer settings object with a custom date format string JsonSerializerSettings settings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" }; // Serialize the DataTable to a JSON string using the custom date format string string json = JsonConvert.SerializeObject(table, settings); // Output the JSON string Console.WriteLine(json);