删除发送到Json MVC的对象的null属性

namespace Booking.Areas.Golfy.Models { public class Time { public string time { get; set; } public int holes { get; set; } public int slots_available { get; set; } public decimal? price { get; set; } public int? Nextcourseid { get; set; } public bool ShouldSerializeNextcourseid { get { return this.Nextcourseid != null; } } public bool? allow_extra { get; set; } public bool ShouldSerializeallow_extra { get { return this.allow_extra != null; } } } } 

 namespace Booking.Areas.Golfy.Controllers { public class TeetimesController : Controller { // // GET: /Golfy/Teetimes/ public ActionResult Index( string date, int? courseid = null, int? clubid = null, int? holes = null, int? available = null, int? prices = null ) { var DateFrom = DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture); Teetimes r = BookingManager.GetBookings(new Code.Classes.Params.ParamGetBooking() { ClubID = clubid , DateFrom = DateFrom , DateTo = DateFrom.AddDays(1) , GroundID = courseid }); return Json(r, JsonRequestBehavior.AllowGet); } } } 

上面的web服务返回一个json字符串,其中包含几个Time of Time。

我希望属性Nextcourseid和allow_extra在它们的值为null时被排除在输出之外。

我试过ShouldSerializeXxx,但似乎没有用。
仅供参考:我也尝试过[ScriptIgnore],但不是有条件的。

您不能使用默认的Json ActionResult来删除null属性。

您可以查看JSON.NET ,它有一个属性,您可以设置该属性,如果它为null,则删除该属性

 [JsonProperty(NullValueHandling=NullValueHandling.Ignore)] 

或者,如果您不想使用其他库,您可以创建自己的json自定义ActionResult并为默认的JavaScriptSerializer注册一个新的转换器,如下所示:

 public class JsonWithoutNullPropertiesResult : ActionResult { private object Data { get; set; } public JsonWithoutNullPropertiesResult(object data) { Data = data; } public override void ExecuteResult(ControllerContext context) { HttpResponseBase response = context.HttpContext.Response; response.ContentType = "application/x-javascript"; response.ContentEncoding = Encoding.UTF8; if (Data != null) { JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.RegisterConverters(new[] { new NullPropertiesConverter() }); string ser = serializer.Serialize(Data); response.Write(ser); } } } public class NullPropertiesConverter : JavaScriptConverter { public override IDictionary Serialize(object obj, JavaScriptSerializer serializer) { var toSerialize = new Dictionary(); foreach (var prop in obj.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Select(p => new { Name = p.Name, Value = p.GetValue(obj) }) .Where(p => p.Value != null)) { toSerialize.Add(prop.Name, prop.Value); } return toSerialize; } public override object Deserialize(IDictionary dictionary, Type type, JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override IEnumerable SupportedTypes { get { return GetType().Assembly.GetTypes(); } } } 

现在在你看来:

 public ActionResult Index() { Teetimes r = BookingManager.GetBookings(); return new JsonWithoutNullPropertiesResult(t); } 

我总是遇到嵌入框架的json序列化程序的问题,因此我使用Json.NET 。 这是测试这两个序列化器的小例子:

 public class Model { public int Id { get; set; } public string Name { get; set; } public bool ShouldSerializeName() { return Name != null; } } class Program { static void Main(string[] args) { var t1 = new Model { Name = "apw8u3rdmapw3urdm", Id = 298384 }; var t2 = new Model { Id = 234235 }; Test(t1); Test(t2); } static void Test(Model model) { Console.WriteLine("JSon from .Net: {0}", ToJson(model)); Console.WriteLine("JSon from JSon.Net: {0}", ToDotNetJson(model)); } static string ToJson(Model model) { var s = new JavaScriptSerializer(); return s.Serialize(model); } static string ToDotNetJson(Model model) { return JsonConvert.SerializeObject(model); } } 

您必须将System.Web.Extensions包含为依赖项,并使用nuget安装Json.Net以使示例正常工作。

这里有一些Json.NET和Framework嵌入式序列化器的文档

给出的答案是有趣的,但我在此期间开始使用DataContractJsonSerializer
并且它可以在不需要使用第三方框架的情况下完成工作(尽管JSON.Net看起来确实被广泛使用)。

 public ActionResult Index( string date , int? courseid = null , int? clubid = null , int? holes = null , int? available = null , int? prices = null ) { var DateFrom = DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture); MTeetimes r = BookingManager.GetBookings(new Code.Classes.Params.ParamGetBooking() { ClubID = clubid , DateFrom = DateFrom , DateTo = DateFrom.AddDays(1) , GroundID = courseid }); // return Json(r, JsonRequestBehavior.AllowGet); string response; var serializer = new DataContractJsonSerializer(typeof(MTeetimes)); // Serialize using (var ms = new MemoryStream()) { serializer.WriteObject(ms, r); response = Encoding.Default.GetString(ms.ToArray()); } return Content(response); } 

 [DataContract] public class Time { [DataMember(Name="time", EmitDefaultValue = false)] public string Time { get; set; } [DataMember(Name = "holes", EmitDefaultValue = false)] public int Holes { get; set; } [DataMember(Name = "slots_available", EmitDefaultValue = false)] public int Slots_available { get; set; } [DataMember(Name = "price", EmitDefaultValue = false)] public decimal? Price { get; set; } [DataMember(Name = "nextcourseid", EmitDefaultValue = false)] public int? Nextcourseid { get; set; } [DataMember(Name = "allow_extra", EmitDefaultValue = false)] public bool? Allow_extra { get; set; } }