将多个 JSON 属性名称映射到 System.Text.Json 中的同一属性

byt*_*dev 7 c# json .net-core system.text.json

我正在编写一个使用 API 的客户端(我无法控制该 API)。不幸的是,API 的不同端点返回命名不一致的 JSON 字段。例如,一个端点正在返回errorCode,但另一个端点可能会返回ErrorCode。

System.Text.Json我在 .NET Core 应用程序中使用它来进行序列化。我想做如下的事情:

public class MyErrorsResponse
{
    [JsonPropertyName("errorCode")]
    [JsonPropertyName("ErrorCode")]
    public string Code { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这在编译时是不允许的,我收到错误:CS0579 Duplicate 'JsonPropertyName' attribute。

无论如何,这个问题有解决办法吗?我尝试了第二个私有设置器的想法,例如:将多个 JsonProperty Name 分配给单个属性。然而,看起来System.Text.Json只序列化公共字段而忽略私有字段。