1. 首页>
  2. 技术文章>
  3. netcore6中webapi使用Newtonsoft.Json做序列化

netcore6中webapi使用Newtonsoft.Json做序列化

2/22/23 2:59:00 PM 浏览 1728 评论 1

netcore json Newtonsoft

现在的net6默认用的是System.Text.Json,这个东西真的是用起来很恼火,如果改回Newtonsoft.Json,可以通过安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet 包并在中进行配置,在Program配置:

builder.Services.AddControllersWithViews().AddNewtonsoftJson(options => {
    options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";//全局处理 返回时间格式
    options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;//全局处理 接收时间并做本地化处理
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();//首字母小写驼峰式命名
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;//忽略循环引用
    //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; 空值处理
});



网友讨论

1楼 6/4/23 2:41:00 PM
确实恼火