例如,我的 class_ 有字符串属性
[DataMember]
[JsonProperty(PropertyName = "email")]
[StringLength(40, ErrorMessage = "The Mobile value cannot exceed 40 characters. ")]
public string Email { get; set; }
由于某种原因,在 Convert.Deserialize 过程中,我需要在此属性中包含空字符串,而不是 null,以防 JSON 对象中未设置此值。 怎么做?
最佳答案
您可以使用DefaultValue属性。
将其装饰为
[DataMember]
[JsonProperty(PropertyName = "email", DefaultValueHandling = DefaultValueHandling.Populate)]
[StringLength(40, ErrorMessage = "The Mobile value cannot exceed 40 characters. ")]
[DefaultValue("")]
public string Email { get; set; }
关于asp.net - Json.Net 如何将 null 反序列化为空字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15136041/