How can I deserialize JSON to a simple Dictionary in ASP.NET?

前端 未结 21 2832
粉色の甜心
粉色の甜心 2020-11-21 06:33

I have a simple key/value list in JSON being sent back to ASP.NET via POST. Example:

{ \"key1\": \"value1\", \"key2\": \"value2\"}

21条回答
  •  我寻月下人不归
    2020-11-21 07:19

    I just implemented this in RestSharp. This post was helpful to me.

    Besides the code in the link, here is my code. I now get a Dictionary of results when I do something like this:

    var jsonClient = new RestClient(url.Host);
    jsonClient.AddHandler("application/json", new DynamicJsonDeserializer());
    var jsonRequest = new RestRequest(url.Query, Method.GET);
    Dictionary response = jsonClient.Execute(jsonRequest).Data.ToObject>();
    

    Be mindful of the sort of JSON you're expecting - in my case, I was retrieving a single object with several properties. In the attached link, the author was retrieving a list.

提交回复
热议问题