繁体   English   中英

如何从 openlibrary api 解析 Json 数据? 使用 Jsoup 和 Gson

[英]How do I parse Json data from openlibrary api? Using Jsoup and Gson

我正在尝试使用 openlibrary 中的书籍 API。 所以,我的问题是,如何抓取数据? 我需要的是抓取标题、作者、出版商和发布日期。

JSON :

{
    "ISBN:0789721813": {
        "bib_key": "ISBN:0789721813",
        "preview": "noview",
        "preview_url": "https://openlibrary.org/books/OL18096553M/Red_Hat_Linux",
        "info_url": "https://openlibrary.org/books/OL18096553M/Red_Hat_Linux",
        "details": {
            "number_of_pages": 757,
            "subtitle": "installation and configuration handbook",
            "latest_revision": 3,
            "contributions": [
                "Hellums, Duane"
            ],
            "title": "Red Hat Linux",
            "languages": [
                {
                    "key": "/languages/eng"
                }
            ],
            "subjects": [
                "Linux",
                "Operating systems (Computers)"
            ],
            "publish_country": "inu",
            "by_statement": "Duane Hellums, et al",
            "type": {
                "key": "/type/edition"
            },
            "revision": 3,
            "other_titles": [
                "Red Hat Linux version 6.0"
            ],
            "publishers": [
                "Que"
            ],
            "last_modified": {
                "type": "/type/datetime",
                "value": "2010-08-18T08:53:00.844526"
            },
            "key": "/books/OL18096553M",
            "publish_places": [
                "Indianapolis, Ind"
            ],
            "pagination": "xix, 757 p. :",
            "created": {
                "type": "/type/datetime",
                "value": "2008-10-10T19:27:28.086386"
            },
            "lccn": [
                "99063852"
            ],
            "notes": {
                "type": "/type/text",
                "value": "\"Red Hat Linux version 6.0.\"--Cover\n\nIncludes index"
            },
            "identifiers": {
                "librarything": [
                    "261776"
                ],
                "goodreads": [
                    "3382689"
                ]
            },
            "isbn_10": [
                "0789721813"
            ],
            "publish_date": "2000"
        }
    }
}

这是我的代码:

class JsonClass {
    public static void main(String[] args) throws IOException {

        org.jsoup.nodes.Document docKb = Jsoup
                .connect("https://openlibrary.org/api/books?bibkeys=ISBN:0789721813&jscmd=details&format=json")
                .ignoreContentType(true).get();
        String json = docKb.body().text();
        String titulo;

        Container fullJsonObject = new Gson().fromJson(json, Container.class);
        for (Details i : fullJsonObject.details) {
            System.out.println("Author: " + i.by_statement);
            System.out.println("Title: " + i.title);
            System.out.println("Editora:  " + i.type.publishers[0]);
            System.out.println("Ano de publicação: " + i.type.notes);
        }

    }

    private class Container {
        Details[] details;
    }

    private class Details {
        String title;
        String by_statement;
        Type type;
    }

    private class Type {
        String publishers[];
        Notes notes;
    }

    private class Notes {
        String publish_date;

    }

}

我试过了,它只是在这一行给了我一个 java.lang.NullPointerException :

 for (Details i : fullJsonObject.details) {   

我很麻木,所以任何答案都可以提供帮助,谢谢。

您的问题是您正在尝试解析一个具有Detail数组的Container ,但响应实际上是一个Map ,其中Container.bib_key作为Container本身作为一个值,因此Map<String, Container> 看来这个 API 方法已经准备好一次返回多个Container

此外Container.details不是一个对象,而是一个单一的值。 因此将Container更改为:

private class Container {
    Details details;
}

并解析正确类型的对象可能会给您更好的结果,例如:

java.lang.reflect.Type type =
        new TypeToken<Map<String, Container>>(){}.getType();        
Map<String, Container> fullJsonObject = new Gson().fromJson(json, type);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM