When using json.net to sequence the class to JSON, you may want to exclude certain attributes when the attribute is empty. You can use to achieve this purpose, but this solution is static, and dynamic exclusion is not allowed according to the attribute value.
Another solution is to use the JsonIgnoreAttribute
attribute of
A single attribute: JsonPropertyAttribute
NullValueHandling
The entire class:
<code class="language-csharp">[JsonProperty("property-name", NullValueHandling=NullValueHandling.Ignore)] public string Property { get; set; }</code>
: Eliminate the attributes of empty values from serialization.
<code class="language-csharp">[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)] public class ClassName { // ... properties ... }</code>
: Apply the same rules to all the attributes in the classes with comments.
Through this method, you can dynamically ignore the attributes of empty, and at the same time still include non -air attributes in the JSON output.The above is the detailed content of How Can I Ignore Null Properties When Serializing with Json.Net?. For more information, please follow other related articles on the PHP Chinese website!