不能让MVC4的WebAPI包括JSON空字段 [英] Can't make MVC4 WebApi include null fields in JSON

查看:309
本文介绍了不能让MVC4的WebAPI包括JSON空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化对象作为JSON与MVC4的WebAPI(RTM - 刚刚安装VS2012 RTM,但今天是昨天中具有RC这个问题),我想对在JSON输出中呈现的所有空值。
像这样的:

I'm trying to serialize objects as JSON with MVC4 WebAPI (RTM - just installed VS2012 RTM today but was having this problem yesterday in the RC) and I'd like for all nulls to be rendered in the JSON output. Like this:

[{"Id": 1, "PropertyThatMightBeNull": null},{"Id":2, "PropertyThatMightBeNull": null}]

但即时得到为

[{"Id":1},{"Id":2}]

我发现这个Q / A 的WebAPI犯规序列化空字段但回答都对我不起作用,或者我没有把握放在哪里了答案。

I've found this Q/A WebApi doesnt serialize null fields but the answer either doesn't work for me or I'm failing to grasp where to put the answer.

下面是我已经试过:


  1. 在Global.asax.cs中的的Application_Start,我说:

  1. In Global.asax.cs's Application_Start, I added:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
json.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include;

这不(似乎)的错误,似乎基于望着我试了下东西实际执行。

This doesn't (seem to) error and seems to actually execute based on looking at the next thing I tried.

在控制器方法(在ApiController的子类),又说:

In a controller method (in a subclass of ApiController), added:

base.Configuration.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
base.Configuration.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include;

我说的#1执行,因为在#2这两个值都已经设置之前,这些线路跑了作为我踩通过。

I say #1 executed because both values in #2 were already set before those lines ran as I stepped through.

在一个绝望的举动(因为我真的不想装饰每个对象的每一个属性)我尝试添加该ATTRIB到为空和无属性:

In a desperation move (because I REALLY don't want to decorate every property of every object) I tried adding this attrib to a property that was null and absent:

[JsonProperty(DefaultValueHandling = DefaultValueHandling.Include,
    NullValueHandling = NullValueHandling.Include)]


这三个产生相同的JSON与空属性省略。

All three produce the same JSON with null properties omitted.

其他注意事项:


  • 在IIS本地运行(试过建于太),Windows 7的RTM VS2012

  • 控制器方法返回列表 - 尝试过的IEnumerable

  • 我想序列化的对象是波苏斯。

推荐答案

我终于碰上了这个<一个href=\"http://forums.asp.net/t/1824580.aspx/1?Serializing+to+JSON+Nullable+Date+gets+ommitted+using+Json+NET+and+Web+API+despite+specifying+NullValueHandling\" rel=\"nofollow\">http://forums.asp.net/t/1824580.aspx/1?Serializing+to+JSON+Nullable+Date+gets+ommitted+using+Json+NET+and+Web+API+despite+specifying+NullValueHandling它描述了我的经历作为固定的RTM在测试中的错误。

I finally came across this http://forums.asp.net/t/1824580.aspx/1?Serializing+to+JSON+Nullable+Date+gets+ommitted+using+Json+NET+and+Web+API+despite+specifying+NullValueHandling which describes what I was experiencing as a bug in the beta that was fixed for the RTM.

虽然我已经安装了VS2012 RTM,我的项目仍在使用所有的测试就用的NuGet包。所以我nugetted(nugot?)更新的一切,一切都很好,现在(用1号从我的问题)。虽然我感觉傻有烧半天。

Though I had installed VS2012 RTM, my project was still using all the nuget packages that the beta came with. So I nugetted (nugot?) updates for everything and all is now well (using #1 from my question). Though I'm feeling silly for having burned half a day.

这篇关于不能让MVC4的WebAPI包括JSON空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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