Json.Net How to deserialize null as empty string?

≯℡__Kan透↙ 提交于 2020-01-11 10:07:24

问题


I have string property in my class_ for example

        [DataMember]
        [JsonProperty(PropertyName = "email")]
        [StringLength(40, ErrorMessage = "The Mobile value cannot exceed 40 characters. ")]
        public string Email { get; set; }

By some reason during Convert.Deserialize process I need to have empty string in this property instead on null in case this value is not setup in JSON object. How to do it ?


回答1:


You could use the DefaultValue attribute.

Decorate it as

[DataMember]
[JsonProperty(PropertyName = "email", DefaultValueHandling = DefaultValueHandling.Populate)]
[StringLength(40, ErrorMessage = "The Mobile value cannot exceed 40 characters. ")]
[DefaultValue("")]
public string Email { get; set; }


来源:https://stackoverflow.com/questions/15136041/json-net-how-to-deserialize-null-as-empty-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!