我正在尝试使用HTTP GET请求在Spring Boot应用程序中使用API,该请求返回以下JSON。我遇到的问题是" playerentry"中包含一个JSON数组。与未命名/未配对的球员和球队信息的水平。对于Spring,通常会为JSON的每一层创建一个java类,并使用@JsonProperty()批注指定从哪个JSON部分生成Java对象。如果没有JSON数组中包含的对的名称,并且不确定如何正确设置playerentry数组和包含的数组对的java类,我就无法使用RestTemplate和RestTemplateBuilder来使用这个JSON。非常感谢任何帮助。
{
"rosterplayers": {
"lastUpdatedOn": "2018-02-25 4:24:30 PM",
"playerentry": [
{
"player": {
"ID": "10138",
"LastName": "Abrines",
"FirstName": "Alex"
},
"team": {
"ID": "96",
"City": "Oklahoma City",
"Name": "Thunder",
"Abbreviation": "OKL"
}
},
{
"player": {
"ID": "9466",
"LastName": "Acy",
"FirstName": "Quincy"
},
"team": {
"ID": "84",
"City": "Brooklyn",
"Name": "Nets",
"Abbreviation": "BRO"
}
},
{
"player": {
"ID": "9390",
"LastName": "Adams",
"FirstName": "Steven"
},
"team": {
"ID": "96",
"City": "Oklahoma City",
"Name": "Thunder",
"Abbreviation": "OKL"
}
},
{
"player": {
"ID": "9375",
"LastName": "Afflalo",
"FirstName": "Arron"
},
"team": {
"ID": "103",
"City": "Sacramento",
"Name": "Kings",
"Abbreviation": "SAC"
}
},
{
"player": {
"ID": "9357",
"LastName": "Ajinca",
"FirstName": "Alexis"
},
"team": {
"ID": "110",
"City": "New Orleans",
"Name": "Pelicans",
"Abbreviation": "NOP"
}
},
{
"player": {
"ID": "9272",
"LastName": "Aldrich",
"FirstName": "Cole"
},
"team": {
"ID": "100",
"City": "Minnesota",
"Name": "Timberwolves",
"Abbreviation": "MIN"
}
},
{
"player": {
"ID": "9480",
"LastName": "Aldridge",
"FirstName": "LaMarcus"
},
"team": {
"ID": "106",
"City": "San Antonio",
"Name": "Spurs",
"Abbreviation": "SAS"
}
},
{
"player": {
"ID": "9454",
"LastName": "Alexander",
"FirstName": "Cliff"
},
"team": {
"ID": "95",
"City": "Orlando",
"Name": "Magic",
"Abbreviation": "ORL"
}
},
{
"player": {
"ID": "9299",
"LastName": "Allen",
"FirstName": "Tony"
},
"team": {
"ID": "107",
"City": "Memphis",
"Name": "Grizzlies",
"Abbreviation": "MEM"
}
}
]
}
}
答案 0 :(得分:2)
这应该有效
class Roasterplayers {
String lastUpdatedOn;
List<PlayerEntry> playerentry;
}
class PlayerEntry {
Player player;
Team team;
}
class Player {
@JsonProperty("ID")
String id;
@JsonProperty("LastName")
String lastName;
@JsonProperty("FirstName")
String firstName;
}
class Team {
@JsonProperty("ID")
String id;
@JsonProperty("City")
String city;
@JsonProperty("Name")
String name;
@JsonProperty("Abbreviation")
String abbreviation;
}
确保每个字段都有Setter和Getters