在WCF中返回空字符串而不是“null”

时间:2013-09-03 23:31:44

标签: wcf

我创建了返回json的Wcf服务点。 我的问题是如何在json中返回空字符串而不是null。

{
 "Avatar": null // send only -> ""
 "Email": null,
 "FirstName": "John",
 "ID": 1,
 "LastName": "Travolta",
 "NickName": null,
 "Password": null
}

------- EDIT ---------

    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }

    [DataMember]
    public string NickName { get; set; }

    [DataMember]
    public string Email { get { return string.IsNullOrEmpty(_email) ? "" : _email; } set { _email = value; } }

    private string _email;

    [DataMember]
    public string Password { get; set; }

    [DataMember]
    public string Avatar { get; set; }

1 个答案:

答案 0 :(得分:1)

{
 "Avatar": EmptyIfNull(null),
 "Email": EmptyIfNull(null),
 "FirstName": "John",
 "ID": 1,
 "LastName": "Travolta",
 "NickName": EmptyIfNull(null),
 "Password": EmptyIfNull(null),
}

private string EmptyIfNull(string source)
{
    return source ?? String.Empty;
}