【发布时间】:2020-02-02 03:15:00
【问题描述】:
我正在我的 .NET Core 3.0 应用程序中从 Newtonsoft.Json 迁移到 System.Text.Json。我试图忽略错误值。
在System.Text.Json 我找到了忽略空值的选项:
JsonSerializerOptions.IgnoreNullValues = true;
但我在System.Text.Json 中找不到忽略错误值的选项。
有人知道如何使用System.Text.Json 实现这一点吗?
或者,如果有人知道相当于 Newtonsoft DefaultValueHandling = DefaultValueHandling.Ignore 的选项,那也太棒了。
【问题讨论】:
-
如果我理解你的正确,我认为你必须使用属性 [Default(false)]。
-
感谢@AndreasSchmidt 属性
[DefaultValue(false)]来自Newtonsoft.Json。我想使用System.Text.Json -
我很确定它不是来自 Newtonsoft.Json:docs.microsoft.com/en-us/dotnet/api/…
-
抱歉你是对的@AndreasSchmidt!它实际上来自
System.ComponentModel命名空间。不幸的是[DefaultValue(false)]不起作用。 -
@dbc 不幸的是,这不是真的。你只负责写值,属性名已经写好了。所以如果你不写,你会得到一个无效的 JSON
标签: c# json .net-core json.net system.text.json