如果 c# 中的整数值为 0,则忽略对象的属性

Aam*_*mir 0 c# asp.net asp.net-core

如果整数属性为 0 忽略它,来自 json 对象。

[JsonProperty("amount")]
public int Amount { get; set; }
Run Code Online (Sandbox Code Playgroud)

我不想在应用程序中全局应用它。

小智 5

这可以使用Json-Ignore 属性来实现。

代码将是:

[JsonProperty("amount")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int Amount { get; set; }
Run Code Online (Sandbox Code Playgroud)