ASP.NET MVC:Json(IDictionary )转换为键值对数组

时间:2022-02-18 19:01:37

If I have an IDictionary<string, string> MyDictionary that resembles this:

如果我有一个IDictionary MyDictionary,它类似于: ,string>

{
    {"foo", "bar"},
    {"abc", "xyz"}
}

and in my MVC controller I have a method like so:

在我的MVC控制器中,我有一个像这样的方法:

[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

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.

因此,这与使用自定义JSON序列化程序的ASP.NET 2.0应用程序升级此项目的事实直接相关;显然,为了向后兼容,他们在MVC应用程序中使用了默认的JSON序列化程序。最终,我使用Json.NET结果在我的控制器中覆盖了这种行为,我的问题得到了解决。

1 个解决方案

#1


4  

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

使用默认的Json序列化程序(Json.Net),它应该从Dictionary 返回以下JSON结构 ,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.

在MVC5和MVC6中验证了这一点。

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

如果您仍然遇到问题,为什么不创建一个具有您想要的属性的简单POCO?

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.

并创建一个对象,设置属性值并将其作为JSON发送。

[HttpPost]
public JsonResult DoStuff()
{
  var item = new KeyValueItem
  {
      Foo="Bee",
      Abc="Scoo"
  };
  return Json(item );
}

#1


4  

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

使用默认的Json序列化程序(Json.Net),它应该从Dictionary 返回以下JSON结构 ,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.

在MVC5和MVC6中验证了这一点。

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

如果您仍然遇到问题,为什么不创建一个具有您想要的属性的简单POCO?

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.

并创建一个对象,设置属性值并将其作为JSON发送。

[HttpPost]
public JsonResult DoStuff()
{
  var item = new KeyValueItem
  {
      Foo="Bee",
      Abc="Scoo"
  };
  return Json(item );
}