JSON.NET serialize the empty attributes
To ignore the test2list attribute (if its value is NULL), please use the
NULLVALUEHANDling attribute application of JSONPROPERTY.
Through this modification, if the test2list is null, it will be excluded from the JSON output. Otherwise, it will be included.<code>[JsonProperty("id")] public string ID { get; set; } [JsonProperty("label")] public string Label { get; set; } [JsonProperty("url")] public string URL { get; set; } [JsonProperty("item", NullValueHandling = NullValueHandling.Ignore)] public List<test2> Test2List { get; set; }</code>
alternative: nullValuehandling
Another method is to use nullValuehandling:
This grammar and conditioned JSONIGNORE attributes have the same effect.
<code>[JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public List<test2> Test2List { get; set; }</code>
To ignore the NULL attributes of all attributes in the class, use the JSONObject attribute with ItemnullValueHandling:
Through this configuration, the attributes of any value in TEST1 will be excluded from JSON serialization.The above is the detailed content of How to Exclude Null Properties During JSON Serialization with Json.NET?. For more information, please follow other related articles on the PHP Chinese website!