C#重命名JSON动态属性名

C#重命名JSON动态属性名,c#,json,deserialization,json-deserialization,C#,Json,Deserialization,Json Deserialization,我使用json格式从服务器获取匹配数据。在这个json中,有两个属性(键),它们的名称是可变的和动态的,并且取决于设备。 例如,我获得了fixture_id 256的匹配数据 { "api": { "results": 1, "fixtures": [ { "fixture_id": 256, "league_id": 21, "homeTeam": { "team_id": 12,

我使用json格式从服务器获取匹配数据。在这个json中,有两个属性(键),它们的名称是可变的和动态的,并且取决于设备。 例如,我获得了fixture_id 256的匹配数据

{
  "api": {
    "results": 1,
    "fixtures": [
      {
        "fixture_id": 256,
        "league_id": 21,
        "homeTeam": {
          "team_id": 12,
          "team_name": "Tottenham"
        },
        "awayTeam": {
          "team_id": 13,
          "team_name": "Everton"
        },
        "lineups": {
          "Tottenham": {
            "coach": "qqqq",
            "formation": "4-2-3-1"
          },
          "Everton": {
            "coach": "rrrr",
            "formation": "4-2-3-1"
          }
        }
      }
    ]
  }
}
此json的类

public class rootMatch
{
    [JsonProperty("api")]
    public Api Api { get; set; }
}

public class Api
{
    [JsonProperty("results")]
    public long Results { get; set; }

    [JsonProperty("fixtures")]
    public List<Fixture> Fixtures { get; set; }
}

public partial class Fixture
{
    [JsonProperty("fixture_id")]
    public long FixtureId { get; set; }

    [JsonProperty("league_id")]
    public long LeagueId { get; set; }

    [JsonProperty("homeTeam")]
    public Team HomeTeam { get; set; }

    [JsonProperty("awayTeam")]
    public Team AwayTeam { get; set; }

    [JsonProperty("lineups")]
    public Lineups Lineups { get; set; }
}
public class Lineups
{
    [JsonProperty("Tottenham")]
    public Team Tottenham{ get; set; }

    [JsonProperty("Everton")]
    public Team Everton{ get; set; }
}
public class Team
{
    [JsonProperty("coach")]
    public string Coach { get; set; }

    [JsonProperty("formation")]
    public string Formation { get; set; }
}
公共类根匹配
{
[JsonProperty(“api”)]
公共Api{get;set;}
}
公共类Api
{
[JsonProperty(“结果”)]
公共长结果{get;set;}
[JsonProperty(“固定装置”)]
公共列表装置{get;set;}
}
公共部分类固定装置
{
[JsonProperty(“夹具id”)]
公共长FixtureId{get;set;}
[JsonProperty(“联盟id”)]
公共长联盟ID{get;set;}
[JsonProperty(“homeTeam”)]
公共团队主队{get;set;}
[JsonProperty(“awayTeam”)]
公共团队AwayTeam{get;set;}
[JsonProperty(“阵容”)]
公共队列队列{get;set;}
}
公开课阵容
{
[JsonProperty(“托特纳姆”)]
托特纳姆公共队{get;set;}
[JsonProperty(“埃弗顿”)]
公共团队埃弗顿{get;set;}
}
公开课小组
{
[JsonProperty(“coach”)]
公共字符串Coach{get;set;}
[JsonProperty(“组建”)]
公共字符串格式{get;set;}
}
但是,rootMatch类仅适用于此json。
是否有方法在反序列化期间更改数据属性的名称,将其更改为静态名称

首发阵容中的第一队名->更改为主队

第二个队名在阵容->更改为AwayTeam

例如,在这个json“列表”中

(托特纳姆转换->主队)
和 (Everton convert->AwayTeam)

有问题的JSON是一个具有两个属性的对象,其中属性名称是团队名称。这些都是事先不知道的。要处理此问题,可以将类型更改为具有字符串键的字典

为了创建有意义的名称,我稍微调整了您的类型:

这是一个团队:

public class Team
{
    [JsonProperty("team_id")]
    public int TeamId { get; set; }

    [JsonProperty("team_name")]
    public string TeamName { get; set; }
}
这是一个阵容:

public class Lineup
{
    [JsonProperty("coach")]
    public string Coach { get; set; }

    [JsonProperty("formation")]
    public string Formation { get; set; }
}
然后,夹具将变为:

public partial class Fixture
{
    [JsonProperty("fixture_id")]
    public long FixtureId { get; set; }

    [JsonProperty("league_id")]
    public long LeagueId { get; set; }

    [JsonProperty("homeTeam")]
    public Team HomeTeam { get; set; }

    [JsonProperty("awayTeam")]
    public Team AwayTeam { get; set; }

    [JsonProperty("lineups")]
    public Dictionary<string, Lineup> Lineups { get; set; }
}
团队名称在字典中用作键

我使用
First
拾取第一个夹具。您可能会使用LINQ或循环来处理所有装置。

您可以添加属性

[JsonExtensionData]
public Dictionary<string, object> AdditionalData { get; set; }
[JsonExtensionData]
公共字典附加数据{get;set;}

这将包含所有无法匹配的属性。然后,您可以迭代字典并根据自己的喜好处理这些数据。

您的要求并不太清楚,但是如果您将
列表
(在
Fixture
)更改为
字典
,它可能会反序列化为更有用的结果,并可能解决您试图实现的问题。
[JsonExtensionData]
public Dictionary<string, object> AdditionalData { get; set; }