【发布时间】:2015-11-29 19:43:45
【问题描述】:
我正在使用 .NET 4.0、MVC 4、Web API。我有以下数据结构:
Dictionary<Actor, Int32> TopActorToMovieCount = new Dictionary<Actor, Int32>(10);
WebApiConfig 中的以下条目:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
在我的控制器中,我以这种方式返回TopActorToMovieCount:
[HttpGet]
public HttpResponseMessage HighestMovies()
{
return Request.CreateResponse(HttpStatusCode.OK, MvcApplication.TopActorToMovieCount);
}
但它给出的 JSON 输出是:
{"api.Models.Actor":137,"api.Models.Actor":125,"api.Models.Actor":99,"api.Models.Actor":96,"api.Models.Actor":83,"api.Models.Actor":82,"api.Models.Actor":81,"api.Models.Actor":79,"....
为什么它没有为 Actor 的对象提供 JSON 结构?
我确定我遗漏了一些东西,但我想不通。我尝试添加以下内容,但没有成功:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
PS:当我切换到 XML 输出时,它工作正常。
【问题讨论】:
标签: json asp.net-mvc-4 asp.net-web-api