听起来您应该使用基于ShouldSerialize
的条件属性序列化
所以,如果你有一个这样的课程;
public class Employee
{
public string FirstName { get; set; }
public string Surname { get; set; }
public bool AddressPopulated { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
public string Postcode { get; set; }
public string City { get; set; }
//Add should serialise here
}
如果将AddressPopulated
设置为True,则只想序列化,然后对所有有条件的属性使用此选项,并将它们添加到上面的类中.像这样.
public bool ShouldSerializeStreet1() { return AddressPopulated; }
public bool ShouldSerializeStreet2() { return AddressPopulated; }
public bool ShouldSerializeCity() { return AddressPopulated; }
public bool ShouldSerializePostcode() { return AddressPopulated; }
另请参阅文档
https://www.newtonsoft.com/json/help/html/conditionalproperties.htm