I have some data that I have to serialize to JSON. I am using JSON.NET. My code structure looks like this:
public struct structA { public string Field1; public structB Field2; public structB Field3; } public struct structB { public string Subfield1; public string Subfield2; }
The problem is that my JSON output should ONLY have Field1 OR Field2 OR Field3 - it depends on which field is used (that is, not empty). By default, my JSON looks like this:
{ "Field1": null, "Field2": {"Subfield1": "test1", "Subfield2": "test2"}, "Field3": {"Subfield1": null, "Subfield2": null}, }
I know I can use NullValueHandling.Ignore , but this gives me JSON, which looks like this:
{ "Field2": {"Subfield1": "test1", "Subfield2": "test2"}, "Field3": {} }
And I need this:
{ "Field2": {"Subfield1": "test1", "Subfield2": "test2"}, }
Is there an easy way to achieve this?
Thaven Mar 22 2018-12-12T00: 00Z
source share