Home >Backend Development >C++ >How to Ignore Null Properties in JSON Serialization with Json.Net?
Consider the following categories:
<code class="language-csharp">class Test1 { [JsonProperty("id")] public string ID { get; set; } [JsonProperty("label")] public string Label { get; set; } [JsonProperty("url")] public string URL { get; set; } [JsonProperty("item")] public List<test2> Test2List { get; set; } }</code>Our goal is to exclude it from JSON serialization when
is empty. To this end, we can use the Test2List
options provided by the JsonProperty
attribute of json.net. NullValueHandling
ignore the empty attribute: NullValueHandling
<code class="language-csharp">[JsonProperty("property_name", NullValueHandling=NullValueHandling.Ignore)] public List<test2> Test2List { get; set; } // 或者 [JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)] public class Test1 { // ... }</code>By using these options, you can ensure that the empty attribute is omitted from generated JSON, so as to provide more concise and clear data representation.
The above is the detailed content of How to Ignore Null Properties in JSON Serialization with Json.Net?. For more information, please follow other related articles on the PHP Chinese website!