2

I have list of json data i have deserialize in list of class object but if proerties name does not match with json data it takes value null.

enter image description here Json data come from the url as user dependent user can enter any data then what i will do for validation to handle null when properties not match. sampleJson list of data is like:

[{"adsname":"Francis","adsimageurl":"Andrew Love.jpg","ontop":false,"key":30012647,"onscan":true,"adscode":6689390,"brandname":{"adsbrand":"Beth Moon"},"category":"New ads","adsscription":"Weinstein Jacob Sutton","from":"2016-12-30T00:00:00","to":"2016-12-30T00:00:00"},{"adsname":"McKay","adsimageurl":"Lorraine Spencer.jpg","ontop":false,"key":136301519,"onscan":true,"adscode":346146503,"brandname":{"adsbrand":"Russell Warner"},"category":"New ads","adsscription":"Stanton Thomas Moran","from":"2016-12-30T00:00:00","to":"2016-12-30T00:00:00"},{"adsname":"Berger","adsimageurl":"Lois Norton.jpg","ontop":false,"key":32971839,"onscan":false,"adscode":334075948,"brandname":{"adsbrand":"Becky Park"},"category":"New ads","adsscription":"Gallagher Matthew Pitts","from":"2016-12-30T00:00:00","to":"2016-12-30T00:00:00"},{"adsname":"Boswell","adsimageurl":"Constance Scarborough.jpg","ontop":false,"key":183877654,"onscan":true,"adscode":230154009,"brandname":{"adsbrand":"Yvonne Hardy"},"category":"New ads","adsscription":"Riddle Nancy Atkins","from":"2016-12-30T00:00:00","to":"2016-12-30T00:00:00"}]

My model propeties class like

public class AdsImportEntity
{
    [JsonProperty(PropertyName = "title")]
    public string AdsTitle { get; set; }

    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "barcode")]
    public string Barcode { get; set; }

    [JsonProperty(PropertyName = "top")]
    public bool? Top { get; set; }

    [JsonProperty(PropertyName = "fromdatetime")]
    public System.DateTime? FromDatetime { get; set; }

    [JsonProperty(PropertyName = "todatetime")]
    public DateTime? ToDatetime { get; set; }

    [JsonProperty(PropertyName = "httpimageurl")]
    public string HttpImageUrl { get; set; }
}

My Question is If In list of object if object all properties contain null value then remove from list.

Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26
  • For just instances of `AdsImportEntity` or for any object? – Richard Nov 10 '16 at 10:15
  • For any object from List – Lalji Dhameliya Nov 10 '16 at 10:18
  • you can do this by iterating through object properties. here is an example of how to do that with `propertyinfo` class: [link](http://stackoverflow.com/questions/957783/loop-through-an-objects-properties-in-c-sharp). So you just have to iterate your list and iterate all the properties of list's objects. There you can check if they are null and remove the object. – Jenism Nov 10 '16 at 10:20
  • but if lots of record for import json then its degrade the performance to check every object of every property – Lalji Dhameliya Nov 10 '16 at 10:23
  • than you can maybe check for null values when deserializing json. so you will not remove "null" objects, but instead you will not even add them to the list. – Jenism Nov 10 '16 at 10:27
  • If you are only doing this for specific types (rather than for any type) you can write code against those types (which will be much quicker than reflection). But "For any object from List" does not clarify if you only interested in specific types or not. (Note: objects are instances of types and in cases like this you need to be clear which you are talking about.) – Richard Nov 10 '16 at 11:09

2 Answers2

1

To filter in this specific case you could simply apply a little LINQ:

adsImportEntityList = Converter.Deserialize<List<AdsImportEntity>>(adsJson)
                               .Where(x => !(x.AdsTitle == null
                                             && x.Description == null
                                             && ...))
                               .ToList();

If needed in multiple places the filter could use a help:

static bool NotAllFieldsNull(AdsImportEntity x) {
  return !(x.AdsTitle == null
           && x.Description == null
           && ...);
}

adsImportEntityList = Converter.Deserialize<List<AdsImportEntity>>(adsJson)
                               .Where(NotAllFieldsNull)
                               .ToList();

If this is needed for a know set of types then overload NotAllFieldsNull. If it needs to work for any (reference) type you'll need reflection.

Richard
  • 106,783
  • 21
  • 203
  • 265
0

While I'm sure you could reflect through the properties, I would add another property to the model that checks if there are any values.

e.g.

public bool HasValues
{
    get
    {
        return !string.IsNullOrWhiteSpace(this.AdsTitle) ||
               !string.IsNullOrWhiteSpace(this.Description) ||
               this.ToDateTime.HasValue ||
               ... etc ...
    }
}

Then when I have my list, I would just remove them with Linq like:

adsImportEntityList = adsImportEntityList.Where((e) => e.HasValues).ToList();
Craig H
  • 2,001
  • 1
  • 14
  • 18