【问题标题】:How do I return an empty string in a Null property when converted to JSON?转换为 JSON 时如何在 Null 属性中返回空字符串?
【发布时间】:2011-11-15 21:39:43
【问题描述】:

我有一个 ASP.NET MVC 应用程序,它返回一个使用 Json() 方法转换为 JSON 的 Customer 对象。

return Json(Repository.Customer.Get(id));

Customer 对象的属性之一是 Customer.Gender。如果 Gender 属性包含 Null 值,则客户端收到的 JSON 对象包含

Gender: "<null>"

有没有办法让具有 Null 值的属性在 JSON 对象中包含空字符串而不是“null”文本?

【问题讨论】:

标签: c# asp.net .net asp.net-mvc


【解决方案1】:

修改您的客户对象属性?

public class Customer
{
  public string GenderOrEmptyString {get {return this._gender ?? ""; }
}

【讨论】:

  • 是的,它会起作用,但由于我几乎完全使用自动属性声明,因此进行更改将需要大量工作。
猜你喜欢
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
相关资源
最近更新 更多