1

How can I remove a property from an list/array of a object?

My model contains:

 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string Age { get; set; }
 public string Size { get; set; }

And i'm converting this to JSON with

string json = JsonConvert.SerializeObject(client);

And all works fine... I got all propertys of the object in JSON.

But I need to create two levels of acess, and show all info in one level, and the other level, fewer propertys...

My question is: Can I remove a property of an object?

Someting like this:

List<Customer> customers = new List<Customer>();
//(and this have 100 clients inner)

customer.removeProperty(Age, Size); // Can I have someting like this?
Drakron
  • 85
  • 1
  • 6
  • Do you want to remove the property from the model or only from the JSON? If the latter: you can use the attribute `JsonIgnore`([see here](https://www.newtonsoft.com/json/help/html/PropertyJsonIgnore.htm)) – mu88 Nov 14 '19 at 13:27
  • 1
    check this. Hope it is duplicate https://stackoverflow.com/questions/10169648/how-to-exclude-property-from-json-serialization – Jayakumar Thangavel Nov 14 '19 at 13:27
  • https://learn.microsoft.com/en-us/dotnet/api/system.web.script.serialization.scriptignoreattribute?redirectedfrom=MSDN&view=netframework-4.8 – Jayakumar Thangavel Nov 14 '19 at 13:30
  • Does this answer your question? [How to exclude property from Json Serialization](https://stackoverflow.com/questions/10169648/how-to-exclude-property-from-json-serialization) – Arijit Mukherjee Nov 14 '19 at 13:35

3 Answers3

3

Try using the json ignore tag above the property. E.g.:

[JsonIgnore]
public string Age { get; set; }

If you want to be able to serialise both with and without certain properties, without altering the structure of your actual classes, you could try using the example here.

Ross Gurbutt
  • 969
  • 1
  • 8
  • 15
0

Create a new model with your fewer properties. Make two lists, one with all the properties and the other with the properties you want removed.

For example

public YourModelNow
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }
    public string Size { get; set; }
}

public YourModelNowFiltered
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

This is assuming you need two file.json with the different properties. If you just need to ignore the properties use Ross Gurburts approach.

Josie G. Bigler
  • 367
  • 4
  • 14
  • I thought about that :/ but i'm in the deadline day and the real code are really complex with more than 100 propertys... and to populate again all this will gonna be really hard :/ – Drakron Nov 14 '19 at 13:33
0

i would suggest using AutoMapper

you will need to create a DTO [Data Transfer Object] class which would have the properties that you would like to expose

the usage is pretty simple:

in the config:

var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<YourModel, YourModelDTO>();
        });

in the actual usage:

IMapper iMapper = config.CreateMapper();
var source = new YourModel();
var destination = iMapper.Map<YourModel, YourModelDTO>(source);

for more information and example of what AutoMapper could do read here

Dor Lugasi-Gal
  • 1,430
  • 13
  • 35