我正在使用 Spring 开发我的 Web 应用程序后端。特别是,我的应用程序管理有关足球队及其球员的数据。
我的应用程序与第三方 REST API 交互以获取球队和球员数据。
对于团队,我创建了一个 Team
实体,如下所示:
@Data
@Table(name = "team")
@Entity
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;
private String name;
private String logoUrl;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "team")
private Set<Player> players;
}
但是,来自 API 的响应具有特定结构,并且在“响应”节点中包含一组团队。
这是响应的结构:
{
"get":"teams",
"parameters":{
"league":"135",
"season":"2020"
},
"errors":[
],
"results":20,
"paging":{
"current":1,
"total":1
},
"response":[
{
"team":{
"id":487,
"name":"Lazio",
"country":"Italy",
"founded":1900,
"national":false,
"logo":"https:\/\/media.api-sports.io\/football\/teams\/487.png"
},
"venue":{
"id":910,
"name":"Stadio Olimpico",
"address":"Viale dei Gladiatori, 2 \/ Via del Foro Italico",
"city":"Roma",
"capacity":68530,
"surface":"grass",
"image":"https:\/\/media.api-sports.io\/football\/venues\/910.png"
}
},
{
"team":{
"id":488,
"name":"Sassuolo",
"country":"Italy",
"founded":1922,
"national":false,
"logo":"https:\/\/media.api-sports.io\/football\/teams\/488.png"
},
"venue":{
"id":935,
"name":"MAPEI Stadium - Citt\u00e0 del Tricolore",
"address":"Piazza Azzuri d'Italia, 1",
"city":"Reggio nell'Emilia",
"capacity":23717,
"surface":"grass",
"image":"https:\/\/media.api-sports.io\/football\/venues\/935.png"
}
},
... // Other team objects
]
}
如何使用 Jackson 库解析答案以获得 List<Team>
?
答案 0 :(得分:2)
您应该为 Jackson 创建匹配结果结构的类,然后将这些类的实例转换为您的 Team
类。对 JPA 实体和 Jackson 反序列化使用相同的类是个坏主意。
有些在线服务允许生成这样的类。例如这个 https://json2csharp.com/json-to-pojo 生成的类是这样的:
// import com.fasterxml.jackson.databind.ObjectMapper; // version 2.11.1
// import com.fasterxml.jackson.annotation.JsonProperty; // version 2.11.1
/* ObjectMapper om = new ObjectMapper();
Root root = om.readValue(myJsonString), Root.class); */
public class Parameters{
public String league;
public String season;
}
public class Paging{
public int current;
public int total;
}
public class Team{
public int id;
public String name;
public String country;
public int founded;
public boolean national;
public String logo;
}
public class Venue{
public int id;
public String name;
public String address;
public String city;
public int capacity;
public String surface;
public String image;
}
public class Response{
public Team team;
public Venue venue;
}
public class Root{
public String get;
public Parameters parameters;
public List<Object> errors;
public int results;
public Paging paging;
public List<Response> response;
}
答案 1 :(得分:1)
正如@Sankozi 所说,您可以对您的 java pojo 进行建模以进行 json 反序列化。 然后使用 ObjectMapper 进行反序列化,例如:
ObjectMapper mapper = new ObjectMapper();
CollectionType javaType = mapper.getTypeFactory()
.constructCollectionType(List.class, Response.class);
List<Response> asList = mapper.readValue(jsonArray, javaType);
List<Team> teams = asList.stream()
.flatMap(response -> response.getTeam())
.collect(Collectors.toList());