【问题标题】:Getting Thumbnails from Google books api从 Google 图书 api 获取缩略图
【发布时间】:2020-07-07 19:11:48
【问题描述】:

我无法在此 Json 响应中提取图书缩略图的网址: https://www.googleapis.com/books/v1/volumes?q=java&maxResults=10

当我将链接硬编码到 String 值时,它可以工作,所以它一定是我获取缩略图的方法。

这是我的代码:

try {

    // Create a JSONObject from the JSON response string
    JSONObject baseJsonResponse = new JSONObject(bookwormJSON);

    // Extract the JSONArray associated with the key called "items",
    // which represents a list of Books.
    JSONArray bookwormArray = baseJsonResponse.getJSONArray("items");

    // For each bookworm in the bookwormArray, create an {@link Book} object
    for (int i = 0; i < bookwormArray.length(); i++) {
        JSONObject volumeInfo = bookwormArray.getJSONObject(i).getJSONObject("volumeInfo");

        String author = "dork";

        // Extract the value for the key called "title"
        String title = volumeInfo.getString("title");

        // Extract the value for the key called "author - I just get first one in array"
        JSONArray authors = volumeInfo.getJSONArray("authors");

        author = authors.getString(0);

        String imgUrl = volumeInfo.getString("smallThumbnail");
        imgUrl = imgUrl.substring(0, 4) + 's' + imgUrl.substring(4);

        // title, author and url from the JSON response.
        Book bookworm = new Book(title, author,imgUrl ); 

        // When url hardcoded and above related code to url is removed it works.

        // Add the new {@link Earthquake} to the list of Books.
        bookworms.add(bookworm);
    }
}

【问题讨论】:

    标签: java json


    【解决方案1】:

    我发现这些嵌套的 Json 文件/字符串令人困惑,但我确实通过阅读此处的另一篇文章使上述内容正常工作。如果有人感兴趣,这里是代码:

    enter code here试试{ // 从 JSON 响应字符串创建一个 JSONObject JSONObject baseJsonResponse = new JSONObject(bookwormJSON);

                // Extract the JSONArray associated with the key called "items",
                // which represents a list of Books.
                JSONArray bookwormArray = baseJsonResponse.getJSONArray("items");
                // For each bookworm in the bookwormArray, create an {@link Book} object
                for (int i = 0; i < bookwormArray.length(); i++) {
                    JSONObject volumeInfo =  bookwormArray.getJSONObject(i).getJSONObject("volumeInfo");
        //Below is the key line
                              JSONObject imageLinks = volumeInfo.getJSONObject("imageLinks");
                   // Extract the value for the key called "title"
                    String title = volumeInfo.getString("title");
                    // Extract the value for the key called "author - I just get first one in array"
                    JSONArray authors = volumeInfo.getJSONArray("authors");
                    String author = authors.getString(0);
                    // Extract the value for the key called "smallThumbnail"
                    String imgUrl= imageLinks.getString("smallThumbnail");
                    // insert "s" into http
                    imgUrl = imgUrl.substring(0, 4) + 's' + imgUrl.substring(4);
                    // title, author and url from the JSON response.
                    Book bookworm = new Book(title, author,imgUrl );
                    // Add the new {@link Earthquake} to the list of books.
                    bookworms.add(bookworm);
                }
    

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      相关资源
      最近更新 更多