我有麻煩在正確的格式發送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