如果c#中的值为空,如何忽略列表

时间:2017-03-01 07:56:00

标签: c# list

我的课程如下

public class SubscriptionDetailModel
{

    public SubscriptionDetailModel()
    { 
        cap = new List<Subscriptioninfo>();
        filter = new List<Subscriptioninfofilter>();
        schedule = new List<SubscriptioninfoSchedule>();
    }
    public List<Subscriptioninfo> cap { get; set; }
    public List<Subscriptioninfofilter> filter { get; set; }
    public List<SubscriptioninfoSchedule> schedule { get; set; }

    [IgnoreDataMember]
    public Nullable<int> SubscriptionID { get; set; }
    public string Name { get; set; }
}

public class Subscriptioninfo
{
    public int SubscriptionID { get; set; }
    public Nullable<int> AccountID { get; set; }
    public Nullable<int> SubscriptionCapID { get; set; }
    public Nullable<int> LeadCapTypeId { get; set; }
    public string LeadCapType { get; set; }
    public string Name { get; set; }
    public string SubscriptionDescription { get; set; }
}

public class Subscriptioninfofilter
{
    public Nullable<int> SubscriptionFilterID { get; set; }
    public Nullable<int> SubscriptionFilterTypeID { get; set; }
    public string SubscriptionFilterTypeInputValue { get; set; }
    public string SubscriptionFilterTypeName { get; set; }
    public string SubscriptionFilterTypeOperator { get; set; }
}

public class SubscriptioninfoSchedule
{
    public int SubscriptionScheduleId { get; set; }
    public string DayOfWeek { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public DateTime CreatedDate { get; set; }
    public Nullable<DateTime> ModifiedDate { get; set; }
    public int TimeZoneCode { get; set; }
}

我的输出如下所示

"response":"200","message":"Success","data":[{"cap":[],"filter":[],"schedule":[{"SubscriptionScheduleId":5,"DayOfWeek":"Thursday","StartTime":"2017-01-12T12:00:00","EndTime":"2017-01-12T20:30:00","CreatedDate":"2017-01-11T00:00:00","ModifiedDate":null,"TimeZoneCode":8}],"Name":null}]}

现在我的问题是如何从响应中删除&#34; cap&#34;:[],&#34;过滤&#34;:[] 。 如果Public Nullable<int> SubscriptionID {get;set;}

,我们可以使用IgnoreDataMember

但我不知道如何忽略上面的列表。

我的结果应该如下

"response":"200","message":"Success","data":[{"schedule":[{"SubscriptionScheduleId":5,"DayOfWeek":"Thursday","StartTime":"2017-01-12T12:00:00","EndTime":"2017-01-12T20:30:00","CreatedDate":"2017-01-11T00:00:00","ModifiedDate":null,"TimeZoneCode":8}],"Name":null}]}

3 个答案:

答案 0 :(得分:1)

空列表不是具有空值的属性。后者将被省略,第一个不会。 (这就是为什么在这种情况下设置JSON.NET&#39; s NullValueHandling是无用的。)

&#39;解决方案&#39;是将空列表设置为null。然后JSON序列化器将省略它们。使用JSON.NET时的另一个解决方案是编写自定义序列化程序,从序列化中排除空列表。在我看来,两者都不理想。

答案 1 :(得分:1)

如果您使用自定义json序列化程序,那么很难建议另一种解决方案,与@PatrickHofman建议的不同,无论如何,简单的答案是在序列化之前将属性设置为null

SubscriptionDetailModel obj = //method to get required instance of SubscriptionDetailModel
...
obj.cap = obj.cap != null && obj.cap.Any() ? obj.cap : null;
obj.filter = obj.filter != null && obj.filter.Any() ? obj.filter : null;
//call to your serialize method

如果您使用Json.NET,请查看about conditional serializing

简单地说,如果您可以修改SubscriptionDetailModel类,则按以下方式添加方法:

public bool ShouldSerializecap()
{
    return this.cap != null && this.cap.Any();
}

public bool ShouldSerializefilter()
{
    return this.filter != null && this.filter.Any();
}

答案 2 :(得分:0)

如果你使用Newtonsoft json进行序列化,那么将它包含在类的顶部,这可能是空的。

[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]

或者您可以做的是将对象转换为JSON,您可以使用此设置。

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
var myJson = JsonConvert.SerializeObject(myObject, settings);