我想在android中使用retrofit 2和Gson从这个嵌套json中解组一个播放器列表:
{
"api": {
"status": 200,
"message": "GET players/playerId/44",
"results": 1,
"filters": [
...
],
"players": [
{
"firstName": "Kent",
"lastName": "Bazemore",
"teamId": "29",
"yearsPro": "7",
"collegeName": "Old Dominion",
"leagues": {
"standard": {
"jersey": "24",
"active": "1",
"pos": "G-F"
}
}
}
]
}
}
有人能帮忙吗?
如果您正在使用Retrofit,我建议您查看Gson/Jackson/Moshi转换器。这些有助于将JSON响应转换为普通Java对象。
您应该能够以
的方式访问它。api.getPlayers() -> array of players
首先需要创建一个模型类,然后才能访问特定的键。例如:
public class ResponseItem {
private int status;
private String message;
private List<String> players;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<String> getPlayers() {
return players;
}
public void setPlayers(List<String> players) {
this.players = players;
}
在您的retrofit方法中添加以下代码片段:
if (response.isSuccessful()) {
ResponseItem responseItem;
responseItem = response.body();
}
注:请确保使用相同的模式完成模型类。