所以我使用amiibo api来提取数据,并显示表中返回的内容。现在,我只是将系列放在表格中以确保它有效,但事实并非如此!
这是我的表:
<table class="table table-striped table-hover table-sm">
<thead>
<tr>
<th class="col-sm-1" scope="col">#</th>
<th class="col-sm-11" scope="col">Amiibo</th>
</tr>
</thead>
<c:forEach var="amiibo" items="${amiibos}" varStatus="status">
<tr>
<th scope="row">${status.count}</th>
<td>${amiibo.amiiboSeries}</td>
</tr>
</c:forEach>
</table>
这是我的servlet,它将api数据拉到列表中:
String json = callAmiiboApi(input.getAmiibo());
// Check for valid json format. If false, String contains an error message.
if (json.startsWith("{") || json.startsWith("[")) {
Gson gson = new Gson();
ArrayList<Amiibo> amiibos = gson.fromJson(json, new TypeToken<ArrayList<Amiibo>>(){}.getType());
//get list of elements
if (!amiibos.isEmpty() && amiibos.size() > 0) {
request.setAttribute("amiibos", amiibos);
}
}
这是我的对象:
public class Amiibo implements Serializable
{
public String amiiboSeries;
public String character;
public String gameSeries;
public String head;
public String image;
public String name;
public String[] release;
public String tail;
public String type;
public Amiibo()
{
amiiboSeries = "";
character = "";
gameSeries = "";
head = "";
image = "";
name = "";
release = new String[0];
tail = "";
type = "";
}
public String getAmiiboSeries()
{
return amiiboSeries;
}
这是我得到的错误:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
有谁知道我在提取这些数据时做错了什么?我认为api访问得很好,我相信我的问题在于我如何显示它(可能是我将它放入列表中?)。
- 编辑 -
这是我的输入类,用于获取用户输入
import java.io.Serializable;
public class Input implements Serializable {
private String amiibo;
public Input() {
this("");
}
public Input(String amiibo) {
this.amiibo = amiibo;
}
public String getAmiibo() {
return amiibo;
}
public void setAmiibo(String amiibo) {
this.amiibo = amiibo;
}
}
克里特包装对象:
class AmiiboResponse {
private List<Amiibo> amiibo;
public List<Amiibo> getAmiibo() {
return amiibo;
}
public void setAmiibo(List<Amiibo> amiibo) {
this.amiibo = amiibo;
}
}
并以这种方式反序列化:
List<Amiibo> amiibos = gson.fromJson(json, AmiiboResponse.class).getAmiibo();