【问题标题】:Why is my JSON extracting method only showing 1 output?为什么我的 JSON 提取方法只显示 1 个输出?
【发布时间】:2018-04-01 19:11:41
【问题描述】:

我一直在学习 Android 中的网络部分,并学会了解析 JSON 响应。最近,我做了一个 Android 应用程序,它使用 Google Books API 并以 JSON 的形式获取响应,对于特定查询,示例查询是:

https://www.googleapis.com/books/v1/volumes?q=android

此特定查询输出标题中包含 Android 的所有书籍。

现在,当我在下面编写以下代码时,它应该输出标题、作者姓名、书籍成本和每本书的页数,我要么是一个响应,要么是没有响应。

private static List<Book> extractFeatureFromJson(String jsonResponse) {
    if(TextUtils.isEmpty(jsonResponse)){
        return null;
    }

    List<Book> books = new ArrayList<>();

    try {
        JSONObject baseJSONResponse = new JSONObject(jsonResponse);
        JSONArray bookArray =baseJSONResponse.getJSONArray("items");
        for(int i=0;i<bookArray.length();i++){
            JSONObject currentBook = bookArray.getJSONObject(i);
            JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");

            String bookName = volumeInfo.getString("title");

            JSONArray authorsList = volumeInfo.getJSONArray("authors");
            String authors = "";
            for(int j=0; j<authorsList.length();j++){
                authors += authorsList.getString(j) + ", ";
            }
            authors.substring(0,authors.length()-3);

            JSONObject saleInfo = currentBook.getJSONObject("saleInfo");
            JSONObject listPrice = saleInfo.getJSONObject("listPrice");
            double cost = listPrice.getDouble("amount");
            String currencyCode = listPrice.getString("currencyCode");
            currencyCode = currencyCode +" "+ Integer.toString((int)cost);
            int pageCount = volumeInfo.getInt("pageCount");
            Book book = new Book(bookName, authors, currencyCode, Integer.toString(pageCount));

            books.add(book);
        }
    } catch (JSONException e) {
        Log.e("QueryUtils", "Problem parsing the book JSON results", e);
    }

    return books;
}

谁能解释我哪里出错了?提前致谢。

编辑:检查显示的日志结果时

04-02 00:43:11.003 29601-29617/com.example.vanur.booklistapp E/QueryUtils: Problem parsing the book JSON results
org.json.JSONException: No value for listPrice
    at org.json.JSONObject.get(JSONObject.java:389)
    at org.json.JSONObject.getJSONObject(JSONObject.java:609)
    at com.example.vanur.booklistapp.QueryUtils.extractFeatureFromJson(QueryUtils.java:53)
    at com.example.vanur.booklistapp.QueryUtils.fetchBookData(QueryUtils.java:149)
    at com.example.vanur.booklistapp.BookLoader.loadInBackground(BookLoader.java:30)
    at com.example.vanur.booklistapp.BookLoader.loadInBackground(BookLoader.java:9)
    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
    at android.os.AsyncTask$2.call(AsyncTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)

【问题讨论】:

  • 添加完整的错误日志。出现在“解析书的问题......”字符串之后。还要添加您正在解析的 JSON 的代表性示例。
  • @DataDino JSON 响应是您在搜索 URL googleapis.com/books/v1/volumes?q=android 时得到的,它很长,所以认为粘贴它会更麻烦

标签: android json api parsing networking


【解决方案1】:

这是你的问题:

04-02 00:43:11.003 29601-29617/com.example.vanur.booklistapp E/QueryUtils: Problem parsing the book JSON results 
org.json.JSONException: No value for listPrice

您正在寻找不存在的属性 listPrice,因此 JSONObject.getJSONObject() 根据文档抛出异常。

如果您有一个有时会出现但有时不使用 opt 而不是 get 的字段:

saleInfo.getJSONObject("listPrice");

对比:

saleInfo.optJSONObject("listPrice");

API:https://developer.android.com/reference/org/json/JSONObject.html

然后您只需要检查null 的返回值并执行您需要的任何处理。

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多