Json.Net如何反序列化null作为空字符串? [英] Json.Net How to deserialize null as empty string?

查看:1898
本文介绍了Json.Net如何反序列化null作为空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有例如在我的class_字符串属性。

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; }

通过在Convert.Deserialize过程中的一些原因,我需要在此属性空字符串,而不是对空的情况下,这个值是不是在JSON对象设置。
怎么办呢?

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 ?

推荐答案

您可以使用默认值属性。

装饰为

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

这篇关于Json.Net如何反序列化null作为空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆