iss*_*swf 3 c# asp.net null json.net .net-6.0
新手问题:如何使 JSON 输出忽略null值?我不想一定将每个单独的属性设置为忽略null(如用 装饰每个属性[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]),并且我发现并尝试的几种不同的全局方法不起作用。我正在使用 .Net 6 和 Newtonsoft.Json
我的控制器中有这个方法
[HttpPost]
public async Task<ResponseJson> Post([FromBody] RequestJson value)
{
DataProcessor processor = new DataProcessor(value);
return processor.GetResults();
}
Run Code Online (Sandbox Code Playgroud)
这就是ResponseJson看起来的样子(为简洁起见,省略了一些属性)。
public class ResponseJson
{
[JsonProperty(PropertyName = "items")]
public List<Item> Items { get; set; }
}
public class Item
{
[JsonProperty(PropertyName = "name")]
public string name { get; set; }
[JsonProperty(PropertyName = "colour")]
public string colour { get; set; }
[JsonProperty(PropertyName = "parameters")]
public ItemParameters parameters { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
DataProcessor不设置colour( null),或者根本不设置ItemParameters某些Item. 查看调用此方法的响应时,JSON 字符串如下所示:
{
"items":
[
{
"name":"abc",
"colour": "blue",
"parameters":{<a bunch of parameters>}
},
{
"name":"def",
"colour": null
"parameters":null
},
{
"name":"ghi",
"colour": null,
"parameters":null
},
{
"name":"jkl",
"colour": "red",
"parameters":{<a bunch of parameters>}
}
]
}
Run Code Online (Sandbox Code Playgroud)
null我希望完全忽略具有值的属性,使其看起来像这样:
{
"items":
[
{
"name":"abc",
"colour": "blue",
"parameters":{<a bunch of parameters>}
},
{
"name":"def"
},
{
"name":"ghi"
},
{
"name":"jkl",
"colour": "red",
"parameters":{<a bunch of parameters>}
}
]
}
Run Code Online (Sandbox Code Playgroud)
net core 6 web api,在程序中,AddJsonOptions添加AddControllers
var builder = WebApplication.CreateBuilder(args);\nbuilder.Services.AddControllers().AddJsonOptions(options =>\n{ \n options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;\n});\nRun Code Online (Sandbox Code Playgroud)\n在.net core mvc中
\nservices.AddControllersWithViews().AddJsonOptions(options =>\n{\n options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;\n});\nRun Code Online (Sandbox Code Playgroud)\n用最少的 API 创建服务
\nbuilder.Services.Configure<JsonOptions>(options =>\n{\n // Set this to true to ignore null or default values\n options.SerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;\n});\nRun Code Online (Sandbox Code Playgroud)\n使用最少的 API 完成示例
\nusing Microsoft.AspNetCore.Http.Json;\n\nvar builder = WebApplication.CreateBuilder(args);\n\n// Add services to the container.\n// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle\nbuilder.Services.AddEndpointsApiExplorer();\nbuilder.Services.AddSwaggerGen();\n\nbuilder.Services.Configure<JsonOptions>(options =>\n{\n // Set this to true to ignore null or default values\n options.SerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;\n});\n\nvar app = builder.Build();\n\n// Configure the HTTP request pipeline.\nif (app.Environment.IsDevelopment())\n{\n app.UseSwagger();\n app.UseSwaggerUI();\n}\n\n\napp.MapGet("/GetWeatherForecast", () => new Person { FirstName = "G\xc3\xa9rald"});\napp.Run();\n\npublic class Person\n{\n public string? FirstName { get; set; }\n public string? LastName { get; set; }\n}\nRun Code Online (Sandbox Code Playgroud)\n结果不忽略 null
\n{\n "FirstName": "G\xc3\xa9rald",\n "LastName": null\n}\nRun Code Online (Sandbox Code Playgroud)\n结果忽略空值
\n{\n "firstName": "G\xc3\xa9rald"\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
5607 次 |
| 最近记录: |