【发布时间】:2020-09-13 14:16:41
【问题描述】:
对于列表
@JsonIgnoreProperties(ignoreUnknown = true)
public class LolMatch {
private long gameId;
private String role;
private int season;
private String platformID;
private int champion;
private int queue;
private String lane;
private long timestamp;
public long getGameId() {
return gameId;
}
public void setGameId(long gameId) {
this.gameId = gameId;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public int getSeason() {
return season;
}
public void setSeason(int season) {
this.season = season;
}
public String getPlatformID() {
return platformID;
}
public void setPlatformID(String platformID) {
this.platformID = platformID;
}
public int getChampion() {
return champion;
}
public void setChampion(int champion) {
this.champion = champion;
}
public int getQueue() {
return queue;
}
public void setQueue(int queue) {
this.queue = queue;
}
public String getLane() {
return lane;
}
public void setLane(String lane) {
this.lane = lane;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
回复
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.ArrayList;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
public class LolMatchList {
private int startIndex;
private int totalGames;
private int endIndex;
List<LolMatch> matches;
public LolMatchList() {
matches = new ArrayList<LolMatch>();
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getTotalGames() {
return totalGames;
}
public void setTotalGames(int totalGames) {
this.totalGames = totalGames;
}
public int getEndIndex() {
return endIndex;
}
public void setEndIndex(int endIndex) {
this.endIndex = endIndex;
}
public List<LolMatch> getMatches() {
return matches;
}
public void setMatches(List<LolMatch> matches) {
this.matches = matches;
}
}
示例响应是这样的
{
"startIndex": 0,
"totalGames": 151,
"endIndex": 100,
"matches": [
{
"gameId": 1,
"role": "DUO",
"season": 13,
"platformId": "xx",
"champion": 13,
"queue": 450,
"lane": "MID",
"timestamp": 1589718112737
},
{
"gameId": 2,
"role": "SOLO",
"season": 13,
"platformId": "xx",
"champion": 7,
"queue": 450,
"lane": "BOTTOM",
"timestamp": 1589716370234
},
{
"gameId": 11,
"role": "DUO_SUPPORT",
"season": 13,
"platformId": "xx",
"champion": 55,
"queue": 450,
"lane": "MID",
"timestamp": 1589714562139
}
]
}
这就是我得到响应的方式
URL url = 新 URL(request_url);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
JSONObject jAns = new JSONObject(br.readLine());
JSONArray 匹配 = jAns.getJSONArray("matches");
我无法将响应映射到这些类 LolMatch 和 LolMatchList。有没有办法自动执行此操作,或者我应该解析数据并通过响应来自己创建对象?谢谢
【问题讨论】:
-
您正在将响应读取为行,您应该循环并连接响应。