Skip to main content
 首页 » 编程设计

c#之反序列化键值对

2024年02月23日17birdshome

您好,我从服务器收到类似这样的响应

{"total":110,"responses":{"13":26,"14":24,"15":40,"16":20}} 

我无法使用 DataContractJSONSerializer 反序列化它。我已经尝试了各种组合,但没有结果。 我正在使用以下类进行反序列化:

public class PollResponseRoot 
{ 
    public int total { get; set; } 
    public Dictionary<int, int> Responses; 
 
} 

但我得到的 Response 值总是 null。请让我知道哪里出错了。

请您参考如下方法:

DataContractJsonSerializer 不支持字典,您可以使用 Json.Net 试试运气相反。

也可在 NuGet 上获得:http://nuget.org/List/Packages/Newtonsoft.Json