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

前端 未结 21 2806
粉色の甜心
粉色の甜心 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 06:58

    System.Text.Json

    This can now be done using System.Text.Json which is built-in to .NET Core 3.0. It's now possible to deserialize JSON without using third-party libraries.

    var json = @"{""key1"":""value1"",""key2"":""value2""}";
    var values = JsonSerializer.Deserialize>(json);
    

    Also available in NuGet package System.Text.Json if using .NET Standard or .NET Framework.

提交回复
热议问题