且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

反序列化到JSON在字典中的Web API控制器

更新时间:2023-02-15 09:51:28

试试这个

  [HttpPost] 
公共对象字典(词典< INT,列表与LT; INT>> sendedData)
{
VAR D1 =请求。 Content.ReadAsStreamAsync()结果;
变种rawJson =新的StreamReader(D1).ReadToEnd();
sendedData = Newtonsoft.Json.JsonConvert.DeserializeObject<字典< INT,列表与LT;串>>&GT (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);

    }