How to exclude property from Json Serialization

后端 未结 7 1541
粉色の甜心
粉色の甜心 2020-11-22 13:22

I have a DTO class which I Serialize

Json.Serialize(MyClass)

How can I exclude a public property of it?

(It has to

7条回答
  •  温柔的废话
    2020-11-22 13:34

    You can use [ScriptIgnore]:

    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        [ScriptIgnore]
        public bool IsComplete
        {
            get { return Id > 0 && !string.IsNullOrEmpty(Name); }
        }
    }
    

    Reference here

    In this case the Id and then name will only be serialized

提交回复
热议问题