Can I optionally turn off the JsonIgnore attribute at runtime?

后端 未结 3 880
一生所求
一生所求 2020-11-29 05:41

I am creating a JSON file with Newtonsoft.Json from a set of classes. The file created is very large, so I have created JsonProperty\'s for the properties to re

3条回答
  •  无人及你
    2020-11-29 06:19

    Json support us to ignore property that don't want return. Example

    class Foo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string AlternateName { get; set; }    
    }
    

    How to use it:

    Foo foo = new Foo
    {
        Id = 1,
        Name = "Thing 1",
        AlternateName = null,   
    };
    
    string json = JsonConvert.SerializeObject(foo);
    

提交回复
热议问题