为什么我的JSON提取方法只显示1个输出?

时间:2018-04-01 19:11:42

标签: android json api parsing networking

我一直在学习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)

1 个答案:

答案 0 :(得分:0)

这是你的问题:

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");

VS

saleInfo.optJSONObject("listPrice");

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

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