我正在尝试使用Gson库来解析api响应。这是响应字符串:
{
"data": {
"Thresh": {
"id": 412,
"title": "Zincirli Gardiyan",
"name": "Thresh",
"key": "Thresh"
},
"Aatrox": {
"id": 266,
"title": "Darkin Kılıcı",
"name": "Aatrox",
"key": "Aatrox"
},
"Tryndamere": {
"id": 23,
"title": "Barbar Kral",
"name": "Tryndamere",
"key": "Tryndamere"
},
"Gragas": {
"id": 79,
"title": "Kavgacı Ayyaş",
"name": "Gragas",
"key": "Gragas"
},
"Cassiopeia": {
"id": 69,
"title": "Yılanın Şefkati",
"name": "Cassiopeia",
"key": "Cassiopeia"
},
"type": "champion",
"version": "4.14.2"
}
}
以下是我的课程:
public class Champion {
private int id;
private String title;
private String name;
private String key;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
和响应类:
public class ChampionsResponse {
private String type;
private String version;
private Map<String, Map<String, Champion>> data;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Map<String, Map<String, Champion>> getData() {
return data;
}
public void setData(Map<String, Map<String, Champion>> data) {
this.data = data;
}
}
我使用以下行来使用gson库:
Gson gson = new Gson();
try{
ChampionsResponse championsResponse = gson.fromJson(response, ChampionsResponse.class);
}catch(Exception e){
e.printStackTrace();
}
但我得到例外说“预期BEGIN_OBJECT但在第1行第65栏路径为$ .data ..”。谁能告诉我哪里弄错了?
由于
答案 0 :(得分:1)
我发现了问题,我需要使用
private Map<String, Map<String, String>> data;
而不是
private Map<String, Map<String, Champion>> data;