4

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.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • You can take a look at this post: http://stackoverflow.com/questions/32253259/serialize-dictionary-without-property-name-with-newtonsoft-json – Adrian Stanescu Dec 15 '15 at 21:37
  • That is the dictionary format used by [`DataContractJsonSerializer`](https://stackoverflow.com/questions/4559991/any-way-to-make-datacontractjsonserializer-serialize-dictionaries-properly). Are you using this serializer? See [Json serializers in ASP.NET and other](http://techblog.dorogin.com/2012/05/json-serializers-in-aspnet-and-other.html) 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:45
  • As best I can tell asp.net-mvc doesn't use `DataContractJsonSerializer` by default in any version. Could you have overridden the default behavior? See [ASP.NET MVC: Controlling serialization of property names with JsonResult](http://stackoverflow.com/questions/1302946) 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?](http://stackoverflow.com/questions/7109967). – dbc Dec 15 '15 at 21:55
  • It's entirely possible... there's a lot of weird settings in this project. :) – Jeremy Holovacs Dec 16 '15 at 14:33

1 Answers1

4

With the default Json serializer(Json.Net), It should return you the below JSON structure from a Dictionary<string, string>

{"Foo": "TTTDic", "Bar": "Scoo"}

With your action method:

[HttpPost]
public JsonResult DoStuff()
{
    var MyDictionary = new Dictionary<string, string>();
    MyDictionary.Add("Foo", "TTTDic");
    MyDictionary.Add("Bar", "Scoo");
    return Json(MyDictionary);
}

Verified this in MVC5 and MVC6.

If you are still having problems, why not create a simple POCO with the properties you want?

public class KeyValueItem
{
    public string Foo { set; get; }
    public string Abc { set; get; }
}

And create an object of that, set the property values and send that as JSON.

[HttpPost]
public JsonResult DoStuff()
{
  var item = new KeyValueItem
  {
      Foo="Bee",
      Abc="Scoo"
  };
  return Json(item );
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 2
    Only a note: The POCO would only work if the dictionary is made of a concrete object. Usually people use Dictionaries like that for dynamic values. – jpgrassi Dec 15 '15 at 22:01
  • Exactly my situation; I cannot use a POCO in this case as the "properties" are dynamic. – Jeremy Holovacs Dec 16 '15 at 14:37
  • What version is yours ? I verified it in MVC5 and 6 It converts the dictionary to the JSON format you want. – Shyju Dec 16 '15 at 15:24
  • 1
    Looks like when this application was upgraded to MVC5 from the legacy ASP.NET, they purposely specified a custom json serializer for backwards compatibility (they were using it before the upgrade). I'll mark this as the answer, since it got me on the right track on what to look for. Thanks! – Jeremy Holovacs Dec 21 '15 at 22:03