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";
});
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里面的时间格式化
最后应该这样处理
JsonSerializerSettings settings = new JsonSerializerSettings
{
DateFormatString = "yyyy-MM-dd HH:mm:ss"
};
JArray obj_row = JArray.FromObject(dataTable, JsonSerializer.Create(settings));//
也可以用如下方法
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);
下面是测试代码
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);
网友评论