Default value for missing properties with JSON.net

前端 未结 2 1336
无人共我
无人共我 2020-12-01 06:07

I\'m using Json.net to serialize objects to database.

I added a new property to the class (which is missing in the json in the database) and I want the new property

2条回答
  •  情歌与酒
    2020-12-01 06:36

    You can also have a default value as:

    class Cat
    {           
        public string Name { get; set; }
            
        public int Age { get; set; } = 1 ; // one is the default value. If json property does not exist when deserializing the value will be one. 
    }
    

提交回复
热议问题