【问题标题】:Expected BEGIN_OBJECT but was BEGIN_ARRAY应为 BEGIN_OBJECT,但为 BEGIN_ARRAY
【发布时间】:2013-08-10 14:39:09
【问题描述】:

这是我的 JSON 数据:

   [
        {
        "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 上收到错误消息。这是我的 PageResponce 课程。我想我可能会正确处理信息。我只使用过 JsonObjects。我想知道如何获取“page”:0,然后如何获取“threads”

public class PageResponse {

List<Page> pages;

public List<Page> getPages() {
    return pages;
}

}

我的页面类

公开课页面{

int page;

public int getPage() {
    return page;
}

}

【问题讨论】:

    标签: java json exception gson


    【解决方案1】:

    首先使用JSONArray#getJSONObject()Page 对象拉出数组。

    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 对象。它的List&lt;Page&gt; 现在将与上面显示的反序列化的Page[] 对象匹配。

    PageResponse pageResp = gson.fromJson("{ pages : " + pageJson.toString() + " }",
                                                                  PageResponse.class);
    System.out.println(pageResp.getPages().get(1).getPage()); // 1
    

    现在,如果您也使用 Threads 扩展您的 Page

    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;
        }
    }
    

    整个 JSON 将成功反序列化。

    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
    

    【讨论】:

    • getJSONObject(0) 将为您提供第一个页面对象,该对象将为 getPage() 调用提供 0。
    • @EmmanuelCortes 感谢您的接受。我对PageResponse[] 的使用是不正确的。我现在已经测试并涵盖了所有案例(包括嵌套线程)。喜欢就看看吧。
    • @EmmanuelCortes 那是因为我只将第一页复制为 page:1 (只是更改了它的索引)。我没有完整的 JSON。我的两个页面是相同的。
    • @EmmanuelCortes 为我工作。这是我的source code 和我的data.json 文件。
    • 啊,对不起。我错过了更新。已修复它以供将来参考。
    猜你喜欢
    • 2016-12-27
    • 2017-10-16
    • 1970-01-01
    • 2013-05-15
    • 2017-08-04
    • 2021-05-27
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多