我正在编写 C# 来与第三方 API 交互。我在将特定响应映射到类时遇到问题。
该方法的 doco 表示响应将是:
{
<text>:[{ /* driver ID */
"t":<uint>, /* time of binding/unbinding*/
"u":<long> /* unit ID if binding, 0 if unbinding */
}],
...
}
响应的示例是:
{"2534":[{"t":1711353044,"u":10568}]}
。
我将 Response 类和子类定义为:
public class GetDriverBindingsResponse : IWialonApiRequestObject
{
/// <summary>
/// Collection of bindings. The key is a Driver Id
/// </summary>
[JsonPropertyName("")]
public Dictionary<string, List<DriverUnitBinding>>? Bindings { get; set; }
}
public class DriverUnitBinding
{
/// <summary>
/// time of binding/unbinding
/// </summary>
[JsonPropertyName("t")]
public uint Time { get; set; }
/// <summary>
/// unit ID if binding, 0 if unbinding
/// </summary>
[JsonPropertyName("u")]
public long UnitId { get; set; }
}
但是,反序列化时,Bindings 集合为 null。即使是简单的测试也会失败:
string test = "{\"2534\":[{\"t\":1711353044,\"u\":10568}]}";
GetDriverBindingsResponse? x = JsonSerializer.Deserialize<GetDriverBindingsResponse>(test);
Console.WriteLine(x.Bindings?.Count);
我需要更改什么才能让此类正确反序列化?