你可以通过
NullValueHandling
标志到
JsonProperty
属性(如果您使用的是newtonsoft)。以下是他们文档中的一个示例:
https://www.newtonsoft.com/json/help/html/JsonPropertyPropertyLevelSetting.htm
编辑
尝试创建以下对象结构:
public class Person
{
public string Email { get; set; }
public DateTime CreatedDate { get; set; }
public List<Role> Roles { get; set; }
}
public class Role
{
public string name { get; set; }
public string type { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string town { get; set; }
}
然后将json复制到一个文件中,并像这样反序列化
string json = File.ReadAllText("a.json");
Person person = JsonConvert.DeserializeObject<Person>(json);
编辑
好吧,如果你真的不想看到这个属性,你会得到一些难看的代码。这里有一个简单而肮脏的例子。
public class PersonA
{
public string Email { get; set; }
public DateTime CreatedDate { get; set; }
public List<RoleB> Roles { get; set; }
}
public class RoleB : RoleA
{
public string town { get; set; }
}
public class PersonB
{
public string Email { get; set; }
public DateTime CreatedDate { get; set; }
public List<RoleA> RolesA { get; set; } = new List<RoleA>();
public List<RoleB> RolesB { get; set; } = new List<RoleB>();
}
public class RoleA
{
public string name { get; set; }
public string type { get; set; }
}
然后做这样的事情:
string json = File.ReadAllText("a.json");
PersonA personA = JsonConvert.DeserializeObject<PersonA>(json);
PersonB personB = new PersonB() { Email = personA.Email, CreatedDate = personA.CreatedDate };
foreach(var role in personA.Roles)
{
var roleB = role as RoleB;
if (roleB.town != null)
{
personB.RolesB.Add(roleB);
}
else
{
personB.RolesA.Add(new RoleA() { name = roleB.name, type = roleB.type });
}
}