json - 使用新的 Net Core 3.0 Json 时忽略属性
问题描述
在 ASP.Net Core 2.2 中使用 JSON.Net 时,我能够在序列化为 JSON 时忽略其值为 null 的属性:
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? Created { get; set; }
但是,当使用 JSON (System.Text.Json) 中内置的新 ASP.Net Core 3.0 时,如果其值为 null,我找不到忽略属性的等效属性。
我只能找到 JsonIgnore。
我错过了什么吗?
解决方案
我正在查看.Net Core 3.1,这应该忽略空值
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.IgnoreNullValues = true;
});
请注意,以上不是每个属性/属性,尽管有一个属性可能有用JsonIgnoreAttribute
解决您的问题的替代方法可能是JsonConverterAttribute,有关如何编写自己的转换器的信息在这里
推荐阅读
- excel - 有没有办法计算一个类别的出现次数并在 Excel 中分配一个值?
- amazon-web-services - 快速查看 MLOps 监控
- javascript - 在列表中找到字典的最短方法是什么?
- react-native - 图像未在物理设备中以 react-native 显示
- python - 如何根据字符串列表启动 Python pandas 数据帧?
- android - Android - 特定视图或位置的上方/底部弹出窗口
- amazon-web-services - 每当我编辑解析器时,AppSync 控制台的更新速度非常慢
- angular - Jest 无法使用 ts-gaussian 导入在 Angular 包格式项目中运行测试?
- python - 使用 Pandas 进行分组时按条件选择值
- python - 如果值包含列表中的字符串,如何为 Python 数据框单元格着色