反序列化到JSON在字典中的Web API控制器 [英] Deserialize JSON into dictionary in Web Api controller
本文介绍了反序列化到JSON在字典中的Web API控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的JSON字符串:
{1:[1,3,5],2:[2,5,6],3: 5,6,8]}
我想将它发送到Web API控制器不使用Ajax请求改变:
$阿贾克斯({
型:POST,
网址:阿比/串行/快译通,
数据:JSON.stringify(sendedData),
数据类型:JSON
});
在网页API我有这样的方法:
<前类=郎-CS prettyprint-覆盖>
[HttpPost]
公共对象字典(词典< INT,列表与LT; INT>> sendedData)
{
//代码放在这里
返回NULL;
}
和总 sendedData == NULL。
换句话说:我不知道如何反序列化JSON到(词典< INT,列表与LT; INT>>
。
感谢您的回答。
解决方案
试试这个
[HttpPost]
公共对象字典(词典< INT,列表与LT; INT>> sendedData)
{
VAR D1 =请求。 Content.ReadAsStreamAsync()结果;
变种rawJson =新的StreamReader(D1).ReadToEnd();
sendedData = Newtonsoft.Json.JsonConvert.DeserializeObject<字典< INT,列表与LT;串>>> (rawJson);
}
I have such JSON string:
'{"1":[1,3,5],"2":[2,5,6],"3":[5,6,8]}'
I want to send it to the Web Api Controller without changing using ajax request:
$.ajax({
type: "POST",
url: "Api/Serialize/Dict",
data: JSON.stringify(sendedData),
dataType: "json"
});
In Web Api I have such method:
[HttpPost]
public object Dict(Dictionary<int, List<int>> sendedData)
{
//code goes here
return null;
}
And always sendedData == null.
Another words: I don't know how to deserialize JSON into (Dictionary<int, List<int>>
.
Thank you for answer.
解决方案
Try this
[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);
}
这篇关于反序列化到JSON在字典中的Web API控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文