If I have an IDictionary<string, string> MyDictionary
that resembles this:
{
{"foo", "bar"},
{"abc", "xyz"}
}
and in my MVC controller I have a method like so:
[HttpPost]
public JsonResult DoStuff()
{
return Json(MyDictionary);
}
...it sends back something like:
[
{"Key":"foo", "Value":"bar"},
{"Key":"abc", "Value":"xyz"}
]
I was expecting (and wanting) something like:
{
"foo":"bar",
"abc":"xyz"
}
How can I accomplish this?
UPDATE
So this is directly related to the fact that this project was upgraded from an ASP.NET 2.0 application that used a custom JSON serializer; apparently, for backwards compatibility, they made that the default JSON serializer in the MVC application. Ultimately I overrode that behavior in my controller with the Json.NET result, and my problem was solved.
DataContractJsonSerializer
. Are you using this serializer? See Json serializers in ASP.NET and other for a list of serializers used in various asp.net platforms. Which exact asp.net technology are you using? – dbc Dec 15 '15 at 21:45DataContractJsonSerializer
by default in any version. Could you have overridden the default behavior? See ASP.NET MVC: Controlling serialization of property names with JsonResult for an example of this. To use Json.NET instead see Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?. – dbc Dec 15 '15 at 21:55