在 JSTL 页面的 Table 上获取 JSON 数据
Getting JSON Data on Table on JSTL Page
所以我正在使用 amiibo api 来提取数据,并在 table 中显示返回的内容。现在,我只是将系列放在 table 中以确保它有效,但它没有!
这是我的 table:
<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 可以正常访问,我相信我的问题在于我如何显示它(可能是当我将它放入列表时?)。
-- 编辑 --
这是我的输入class来获取用户输入
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();
所以我正在使用 amiibo api 来提取数据,并在 table 中显示返回的内容。现在,我只是将系列放在 table 中以确保它有效,但它没有!
这是我的 table:
<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 可以正常访问,我相信我的问题在于我如何显示它(可能是当我将它放入列表时?)。
-- 编辑 --
这是我的输入class来获取用户输入
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();