When using json.net for serialization, it is a common demand to selectively exclude air attributes. This article discusses several technologies that achieve this goal, especially paying attention to the attributes of ignoring the list objects.
Overview of the question
Suppose you have a class called Test1, which include ID, label, URL, and a list of objects called Test2. You hope to turn Test1 sequences to JSON, but it only includes it when the test2 list is not empty.
If the test2 list is empty, you can use the attribute to exclude it from the JSON output. However, this method has a limitations. If the TEST2 list is not empty, it will always be included even if it contains vacant items.
JSONPROPERTY attributes in combination with nullValuehandling [JsonIgnore]
attributes with together. This allows you to ignore the attribute of the explicit settings to NULL. The following code demonstrates this method:
Set the global NULLVALUEHANDling [JsonProperty]
NullValueHandling=NullValueHandling.Ignore
[JsonProperty("item", NullValueHandling = NullValueHandling.Ignore)] public List<test2> Test2List { get; set; }
The above is the detailed content of How Can I Ignore Null Properties and Lists in JSON Serialization with Json.NET?. For more information, please follow other related articles on the PHP Chinese website!