Home > Backend Development > C++ > How to Exclude Null Properties During JSON Serialization with Json.NET?

How to Exclude Null Properties During JSON Serialization with Json.NET?

Patricia Arquette
Release: 2025-01-28 17:31:08
Original
257 people have browsed it

How to Exclude Null Properties During JSON Serialization with Json.NET?

JSON.NET serialize the empty attributes

When using json.net for json serialization, you may need to ignore a specific attribute of NULL. The following is how to achieve this goal in your scene:

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>
Copy after login

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>
Copy after login
Class level configuration: jsonObject attribute

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template