Java 应为BEGIN\u对象,但为BEGIN\u数组
这是我的JSON数据:Java 应为BEGIN\u对象,但为BEGIN\u数组,java,json,exception,gson,Java,Json,Exception,Gson,这是我的JSON数据: [ { "page": 0, "threads": [ { "no": 498507562, "last_modified": 1375984181 }, { "no": 498503346, "last_modified
[
{
"page": 0,
"threads": [
{
"no": 498507562,
"last_modified": 1375984181
},
{
"no": 498503346,
"last_modified": 1375984243
},
{
"no": 498497523,
"last_modified": 1375984241
},
{
"no": 498496579,
"last_modified": 1375984241
},
{
"no": 498499114,
"last_modified": 1375984240
},
{
"no": 498503169,
"last_modified": 1375984239
},
{
"no": 498497038,
"last_modified": 1375984239
},
{
"no": 498501946,
"last_modified": 1375984238
},
{
"no": 498507181,
"last_modified": 1375984237
},
{
"no": 498505625,
"last_modified": 1375984236
},
{
"no": 498505578,
"last_modified": 1375984242
},
{
"no": 498507346,
"last_modified": 1375984236
},
{
"no": 498497504,
"last_modified": 1375984236
},
{
"no": 498486742,
"last_modified": 1375984234
},
{
"no": 498502590,
"last_modified": 1375984232
}
]
},
{
"page": 1,
"threads": [
{(...so on)
这是我的反序列化代码:
Gson gson = new Gson();
JSONArray pageJson = JsonReader.readJsonArrayFromUrl(
"https://api.4chan.org/b/threads.json");
System.out.println(pageJson.toString());
PageResponse PageResponse = gson.fromJson(pageJson.toString(), PageResponse.class);
我在PageResponse上看到错误。这是我的PageResponse课程。我想我可能处理的信息是对的。我只处理过JSONObject。我想知道如何获取“页面”:0,然后如何获取“线程”
}首先使用JSONArray#getJSONObject()
将页面
对象从数组中拉出
Page page = gson.fromJson(pageJson.getJSONObject(0).toString(), Page.class);
System.out.println(page.getPage()); // 0
要反序列化所有页面,请使用
Page[] pages = gson.fromJson(pageJson.toString(), Page[].class);
System.out.println("Total: " + pages.length); // 2
System.out.println("pages[0] = " + pages[0].getPage()); // pages[0] = 0
或者,向JSON字符串中添加一组额外的{}
,以匹配封闭的PageResponse
对象。它的列表
现在将与上面反序列化显示的页面[]
对象匹配
PageResponse pageResp = gson.fromJson("{ pages : " + pageJson.toString() + " }",
PageResponse.class);
System.out.println(pageResp.getPages().get(1).getPage()); // 1
现在,如果您使用线程扩展页面
类
class Page {
private int page;
private List<Threads> threads;
public int getPage() {
return page;
}
public List<Threads> getThreads() {
return threads;
}
}
class Threads {
private long no;
private String last_modified;
public long getNo() {
return no;
}
public String getLastModified() {
return last_modified;
}
}
getJSONObject(0)将为您提供第一个page对象,该对象将为getPage()调用提供0。@EmmanuelCortes感谢您的接受。不过,我使用的
PageResponse[]
是不正确的。现在我已经测试并涵盖了所有案例(包括嵌套线程)。如果您愿意,请看一看。@EmmanuelCortes这是因为我只将第一页复制为第1页(只是更改了它的索引)。我身上没有完整的JSON。我的两页完全相同。@EmmanuelCortes为我工作。这是我和我的档案。啊,很抱歉。我错过了更新。修正了它以备将来参考。
PageResponse pageResp = gson.fromJson("{ pages : " + pageJson.toString() + " }",
PageResponse.class);
System.out.println(pageResp.getPages().get(1).getPage()); // 1
class Page {
private int page;
private List<Threads> threads;
public int getPage() {
return page;
}
public List<Threads> getThreads() {
return threads;
}
}
class Threads {
private long no;
private String last_modified;
public long getNo() {
return no;
}
public String getLastModified() {
return last_modified;
}
}
System.out.println(pageResp.getPages().get(0).getThreads().size()); // 15
System.out.println(pageResp.getPages().get(0)
.getThreads().get(0).getNo()); // 498507562
System.out.println(pageResp.getPages().get(0)
.getThreads().get(0).getLastModified()); // 1375984181