Reputation: 55
This is my JSON data:
[
{
"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)
This is my code for deserialization:
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);
I get the error on PageResponse. Here is my PageResponce class. I think I might be handling the information right. I've only worked with JsonObjects. I would like to know how to get "page": 0, and then how to get the "threads"
public class PageResponse {
List<Page> pages;
public List<Page> getPages() {
return pages;
}
}
My Page class
public class Page {
int page;
public int getPage() {
return page;
}
}
Upvotes: 3
Views: 3454
Reputation: 51711
Use JSONArray#getJSONObject()
to pull the Page
object out of array first.
Page page = gson.fromJson(pageJson.getJSONObject(0).toString(), Page.class);
System.out.println(page.getPage()); // 0
To deserialize all the pages use
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
Alternatively, add an extra set of {}
to your JSON string to match against enclosing PageResponse
object. Its List<Page>
would now match against the Page[]
objects that were shown deserialized above.
PageResponse pageResp = gson.fromJson("{ pages : " + pageJson.toString() + " }",
PageResponse.class);
System.out.println(pageResp.getPages().get(1).getPage()); // 1
Now, if you extend your Page
class with Threads
as well
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;
}
}
The whole JSON would deserialize successfully.
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
Upvotes: 4