如何为 Json 架构编写 Java 模型 Class?
How can I write Java Model Class for Json Schema?
问题出在 coach
、goalkeepers
和 defender
之类的实体上 我不知道如何为这种响应做出 class
{
"squad": [
[
"coach",
[
{
"id": 78621,
"name": "Gareth Southgate",
"ccode": "ENG",
"cname": "England"
}
]
],
[
"goalkeepers",
[
{
"id": 280287,
"name": "Sam Johnstone",
"ccode": "8659",
"cname": "West Bromwich Albion",
"role": "goalkeepers"
},
{
"id": 303919,
"name": "Jordan Pickford",
"ccode": "8668",
"cname": "Everton",
"role": "goalkeepers"
},
{
"id": 746395,
"name": "Aaron Ramsdale",
"ccode": "8657",
"cname": "Sheffield United",
"role": "goalkeepers"
}
]
],
[
"defenders",
[
{
"id": 159833,
"name": "Kyle Walker",
"ccode": "8456",
"cname": "Manchester City",
"role": "defenders"
},
{
"id": 191869,
"name": "Kieran Trippier",
"ccode": "9906",
"cname": "Atletico Madrid",
"role": "defenders"
},
{
"id": 247761,
"name": "Conor Coady",
"ccode": "8602",
"cname": "Wolverhampton Wanderers",
"role": "defenders"
},
{
"id": 255610,
"name": "Harry Maguire",
"ccode": "10260",
"cname": "Manchester United",
"role": "defenders"
},
{
"id": 263653,
"name": "John Stones",
"ccode": "8456",
"cname": "Manchester City",
"role": "defenders"
},
{
"id": 362694,
"name": "Luke Shaw",
"ccode": "10260",
"cname": "Manchester United",
"role": "defenders"
},
{
"id": 419664,
"name": "Tyrone Mings",
"ccode": "10252",
"cname": "Aston Villa",
"role": "defenders"
},
{
"id": 672469,
"name": "Ben Chilwell",
"ccode": "8455",
"cname": "Chelsea",
"role": "defenders"
},
{
"id": 776151,
"name": "Ben White",
"ccode": "10204",
"cname": "Brighton & Hove Albion",
"role": "defenders"
},
{
"id": 807729,
"name": "Reece James",
"ccode": "8455",
"cname": "Chelsea",
"role": "defenders"
}
]
],
[
"midfielders",
[
{
"id": 156008,
"name": "Jordan Henderson",
"ccode": "8650",
"cname": "Liverpool",
"role": "midfielders"
},
{
"id": 609755,
"name": "Kalvin Phillips",
"ccode": "8463",
"cname": "Leeds United",
"role": "midfielders"
},
{
"id": 654096,
"name": "Declan Rice",
"ccode": "8654",
"cname": "West Ham United",
"role": "midfielders"
},
{
"id": 750032,
"name": "Mason Mount",
"ccode": "8455",
"cname": "Chelsea",
"role": "midfielders"
},
{
"id": 1077894,
"name": "Jude Bellingham",
"ccode": "9789",
"cname": "Borussia Dortmund",
"role": "midfielders"
}
]
],
[
"attackers",
[
{
"id": 194165,
"name": "Harry Kane",
"ccode": "8586",
"cname": "Tottenham Hotspur",
"role": "attackers"
},
{
"id": 246575,
"name": "Raheem Sterling",
"ccode": "8456",
"cname": "Manchester City",
"role": "attackers"
},
{
"id": 312765,
"name": "Jack Grealish",
"ccode": "10252",
"cname": "Aston Villa",
"role": "attackers"
},
{
"id": 612150,
"name": "Dominic Calvert-Lewin",
"ccode": "8668",
"cname": "Everton",
"role": "attackers"
},
{
"id": 696365,
"name": "Marcus Rashford",
"ccode": "10260",
"cname": "Manchester United",
"role": "attackers"
},
{
"id": 815006,
"name": "Phil Foden",
"ccode": "8456",
"cname": "Manchester City",
"role": "attackers"
},
{
"id": 846381,
"name": "Jadon Sancho",
"ccode": "9789",
"cname": "Borussia Dortmund",
"role": "attackers"
},
{
"id": 961995,
"name": "Bukayo Saka",
"ccode": "9825",
"cname": "Arsenal",
"role": "attackers"
}
]
]
]
}
1-熟悉数组和对象
2- 关于 json 格式的知识
3- 如果数组将其保存在集合中,则对象将成为该集合的一部分。
4- https://www.jsonschema2pojo.org/ link 可以在初期帮助你
为 json 模式创建 Java POJO 时,您只需映射字段。对于您的问题,我们可以这样做。
public class SquadDto{
List<Squad> squads;
}
public class Squad{
List<Coach> coach;
List<Goalkeeper> goalkeepers;
List<Defender> defenders;
List<Midfielder> midfielders;
List<Attacker> attackers;
}
public class Coach{
long id;
String name;
String ccode;
String cname;
String role;
}
同样,我们可以为 GoalKeeper 制作 类,..,以便他们映射 JSON 中的字段。
如果您知道您收到的 JSON 文件的格式总是正确的,那么您可以使用上面 Karamveer 的回复。
如果没有,只需使用 Jackson、Gson、json-io 或 Genson 之类的工具来解析您收到的 JSON 文件。
然后您可以使用 JavaPoet 生成具有给定 JSON 值的 类。
问题出在 coach
、goalkeepers
和 defender
之类的实体上 我不知道如何为这种响应做出 class
{
"squad": [
[
"coach",
[
{
"id": 78621,
"name": "Gareth Southgate",
"ccode": "ENG",
"cname": "England"
}
]
],
[
"goalkeepers",
[
{
"id": 280287,
"name": "Sam Johnstone",
"ccode": "8659",
"cname": "West Bromwich Albion",
"role": "goalkeepers"
},
{
"id": 303919,
"name": "Jordan Pickford",
"ccode": "8668",
"cname": "Everton",
"role": "goalkeepers"
},
{
"id": 746395,
"name": "Aaron Ramsdale",
"ccode": "8657",
"cname": "Sheffield United",
"role": "goalkeepers"
}
]
],
[
"defenders",
[
{
"id": 159833,
"name": "Kyle Walker",
"ccode": "8456",
"cname": "Manchester City",
"role": "defenders"
},
{
"id": 191869,
"name": "Kieran Trippier",
"ccode": "9906",
"cname": "Atletico Madrid",
"role": "defenders"
},
{
"id": 247761,
"name": "Conor Coady",
"ccode": "8602",
"cname": "Wolverhampton Wanderers",
"role": "defenders"
},
{
"id": 255610,
"name": "Harry Maguire",
"ccode": "10260",
"cname": "Manchester United",
"role": "defenders"
},
{
"id": 263653,
"name": "John Stones",
"ccode": "8456",
"cname": "Manchester City",
"role": "defenders"
},
{
"id": 362694,
"name": "Luke Shaw",
"ccode": "10260",
"cname": "Manchester United",
"role": "defenders"
},
{
"id": 419664,
"name": "Tyrone Mings",
"ccode": "10252",
"cname": "Aston Villa",
"role": "defenders"
},
{
"id": 672469,
"name": "Ben Chilwell",
"ccode": "8455",
"cname": "Chelsea",
"role": "defenders"
},
{
"id": 776151,
"name": "Ben White",
"ccode": "10204",
"cname": "Brighton & Hove Albion",
"role": "defenders"
},
{
"id": 807729,
"name": "Reece James",
"ccode": "8455",
"cname": "Chelsea",
"role": "defenders"
}
]
],
[
"midfielders",
[
{
"id": 156008,
"name": "Jordan Henderson",
"ccode": "8650",
"cname": "Liverpool",
"role": "midfielders"
},
{
"id": 609755,
"name": "Kalvin Phillips",
"ccode": "8463",
"cname": "Leeds United",
"role": "midfielders"
},
{
"id": 654096,
"name": "Declan Rice",
"ccode": "8654",
"cname": "West Ham United",
"role": "midfielders"
},
{
"id": 750032,
"name": "Mason Mount",
"ccode": "8455",
"cname": "Chelsea",
"role": "midfielders"
},
{
"id": 1077894,
"name": "Jude Bellingham",
"ccode": "9789",
"cname": "Borussia Dortmund",
"role": "midfielders"
}
]
],
[
"attackers",
[
{
"id": 194165,
"name": "Harry Kane",
"ccode": "8586",
"cname": "Tottenham Hotspur",
"role": "attackers"
},
{
"id": 246575,
"name": "Raheem Sterling",
"ccode": "8456",
"cname": "Manchester City",
"role": "attackers"
},
{
"id": 312765,
"name": "Jack Grealish",
"ccode": "10252",
"cname": "Aston Villa",
"role": "attackers"
},
{
"id": 612150,
"name": "Dominic Calvert-Lewin",
"ccode": "8668",
"cname": "Everton",
"role": "attackers"
},
{
"id": 696365,
"name": "Marcus Rashford",
"ccode": "10260",
"cname": "Manchester United",
"role": "attackers"
},
{
"id": 815006,
"name": "Phil Foden",
"ccode": "8456",
"cname": "Manchester City",
"role": "attackers"
},
{
"id": 846381,
"name": "Jadon Sancho",
"ccode": "9789",
"cname": "Borussia Dortmund",
"role": "attackers"
},
{
"id": 961995,
"name": "Bukayo Saka",
"ccode": "9825",
"cname": "Arsenal",
"role": "attackers"
}
]
]
]
}
1-熟悉数组和对象 2- 关于 json 格式的知识 3- 如果数组将其保存在集合中,则对象将成为该集合的一部分。
4- https://www.jsonschema2pojo.org/ link 可以在初期帮助你
为 json 模式创建 Java POJO 时,您只需映射字段。对于您的问题,我们可以这样做。
public class SquadDto{
List<Squad> squads;
}
public class Squad{
List<Coach> coach;
List<Goalkeeper> goalkeepers;
List<Defender> defenders;
List<Midfielder> midfielders;
List<Attacker> attackers;
}
public class Coach{
long id;
String name;
String ccode;
String cname;
String role;
}
同样,我们可以为 GoalKeeper 制作 类,..,以便他们映射 JSON 中的字段。
如果您知道您收到的 JSON 文件的格式总是正确的,那么您可以使用上面 Karamveer 的回复。
如果没有,只需使用 Jackson、Gson、json-io 或 Genson 之类的工具来解析您收到的 JSON 文件。
然后您可以使用 JavaPoet 生成具有给定 JSON 值的 类。