Deserializing with Json.NET: Requiring a property/key to be present

后端 未结 3 1394
北海茫月
北海茫月 2020-12-20 19:20

When using Json.NET to deserialize a JSON string into an object, how do I require a key/property to be present in the JSON stirng, but allow for NULL values?

For exa

3条回答
  •  礼貌的吻别
    2020-12-20 19:45

    Ugh. I should have spend a little more time in the Json.NET documentation...

    The answer is the the Required property of the JsonPropertyAttribute, which is of enum type Newtonsoft.Json.Required

    [JsonProperty(Required = Newtonsoft.Json.Required.AllowNull)]
    public string Description {get; set;}
    

提交回复
热议问题