如何解决我们可以忽略类属性中的C#列表吗?
c#中是否有任何机制可以忽略List如果它为null?
public class VehicleResponse
{
public Pagination pagination { get; set; }
public Vehicles data { get; set; }
public Include include { get; set; }
}
此处“包含”类属性可能为null。如果它为null,那么我想忽略该属性。
public class Include
{
public List<Devices> devices { get; set; }
public List<Institutions> institutions { get; set; }
public List<Cars> cars{ get; set; }
}
这是包含类。如果设备,机构或汽车之类的任何列表为空,则应将其忽略。
我不想在json响应中显示null值。如果它们为null,请忽略它们。
解决方法
您必须创建一个空的构造函数来初始化这些列表,而JSON结果将是空列表。
public class Include
{
public Include(){
devices = new List<Devices>();
institutions = new List<Institutions>();
cars= new List<Cars>();
}
public List<Devices> devices { get; set; }
public List<Institutions> institutions { get; set; }
public List<Cars> cars{ get; set; }
}
编辑
我刚刚找到了这个.NET Core: Remove null fields from API JSON response,希望对您有所帮助。另外,我建议您编写自己的OutputFormatter
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。