Using json.net to ignore the empty attributes in json serialization
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>
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>
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!