记录JObject对象作为API返回对象时数据丢失
ASP.NET Core 默认的JSON处理程序是 System.Text.Json.JsonSerializer,而JObject 对象是NewtonsoftJson定义的,所以默认的JSON处理程序无法处理JObject对象
1 最简单的解决方案就是用NewtonsoftJson替换默认的JSON处理程序
安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson
var builder = WebApplication.CreateBuilder(args); builder.Services .AddControllersWithViews() .AddRazorRuntimeCompilation() .AddNewtonsoftJson();
2 在默认JSON处理程序不变的情况下
2.1 定义实体类去转换JObject
2.2 将JObject 转换成匿名类
JsonConvert.DeserializeAnonymousType(JObject.ToString(), new{p1="",p2=0});