我有这样的 JSON 字符串:
'{"1":[1,3,5],"2":[2,5,6],"3":[5,6,8]}'
我想使用 ajax 请求将它发送到 Web Api Controller 而不更改:
$.ajax({
type: "POST",
url: "Api/Serialize/Dict",
data: JSON.stringify(sendedData),
dataType: "json"
});
在Web Api中我有这样的方法:
[HttpPost]
public object Dict(Dictionary<int, List<int>> sendedData)
{
//code goes here
return null;
}
总是sendedData == null.
换句话说:我不知道如何将 JSON 反序列化为 (Dictionary<int, List<int>>
.
谢谢你的回答。
最佳答案
试试这个
[HttpPost]
public object Dict(Dictionary<int, List<int>> sendedData)
{
var d1 = Request.Content.ReadAsStreamAsync().Result;
var rawJson = new StreamReader(d1).ReadToEnd();
sendedData=Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<int, List<string>>>(rawJson);
}
关于javascript - 在 Web Api Controller 中将 JSON 反序列化为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988273/