默认JSON序列不支持这个开箱即用的,但是JSON.NET和System.Web.Script.Serialization.JavaScriptSerializer
支持这一点,核对答案,以一个类似的问题:
How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
JSON.NET将成为默认的JSON序列ASP.NET 4.5 WebAPI,因此您可以期望它可以随ASP.NET一起提供并从MS获得支持。
UPDATE
为了让ASP.NET模型绑定正常,你需要一个定制的模型绑定:
public class DictionaryModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var serializer = new JavaScriptSerializer();
var value = controllerContext.RequestContext.HttpContext.Request.Form["json"];
return serializer.Deserialize<Dictionary<string, object>>(HttpUtility.UrlDecode(value));
}
}
而新模型绑定添加到Application_Start方法中应用模型联:
protected void Application_Start()
{
ModelBinders.Binders.Add(typeof (Dictionary<string, object>), new DictionaryModelBinder());
RegisterRoutes(RouteTable.Routes);
}
它会帮助,如果你告诉我们*你怎么称呼这个方法... – epoch 2012-03-12 12:05:35
@epoch我通过Fiddler调用它。这些是参数。 – 2012-03-12 12:07:09