我有麻烦在正确的格式发送JSON数据到这个C#方法:传递JSON的C#方法用词典<INT,列表<int>>作为参数
public bool MyMethod(int foo, Dictionary<int, List<int>> bar)
我不知道什么是格式化bar
变量:
var bar = {};
bar['1'] = [1, [1, 2]];
bar['2'] = [1, [1, 2, 3]];
bar['3'] = [1, [1, 2]];
$.ajax({
...
data: '{"foo":1, "bar":' + JSON.stringify(bar) + '}'
});
.NET给了我一个'InvalidOperationException`以下消息:
Type 'System.Collections.Generic.Dictionary is not supported for
serialization/deserialization of a dictionary, keys must be strings or objects.
你在使用什么串行器?如果您使用的是默认的.net序列化,我建议您使用处理字典序列化的JSON序列化程序,并且比默认的序列化程序更高效。 – 2014-09-01 07:42:06
因为您使用ajax发送json对象,请尝试以下链接:http://www.codeproject.com/Articles/773102/Redirect-and-Post-JSON-Object-in-ASP-NET-MVC您可能只需更换带有NewtonSoft JSON序列化程序的默认JavasScriptSerializer。 – 2014-09-01 07:45:29