在Java中使用JSON解析时出现问题?

时间:2016-08-24 22:07:43

标签: java android json

我有一个ListView,其中包含3个TextView,它会显示图书标题,作者和书籍列表API中的发布日期。

现在问题是我可以显示Books标题和日期,但是authors元素在JSONArray中,因为我正在考虑运行一个内部for循环,它会将作者添加到字符串变量中但是因为字符串变量在内部循环中,所以当我尝试将变量传递给Book自定义类时,它是不可见的。

我可以在“StringvolumeInfo中显示JSONObject,这是给我问题的“作者”JSONArray

很抱歉没有明确解释这一点,但我们将不胜感激。

这是我的JSON格式

{  
"kind":"books#volumes",
"totalItems":1045,
"items":[  
  {  
     "kind":"books#volume",
     "id":"RVhsAQAAQBAJ",
     "etag":"6uHhm1spXck",
     "selfLink":"https://www.googleapis.com/books/v1/volumes/RVhsAQAAQBAJ",
     "volumeInfo":{  
        "title":"Android Programming",
        "subtitle":"The Big Nerd Ranch Guide",
        "authors":[  
           "Bill Phillips",
           "Brian Hardy"
        ],
        "publisher":"Pearson Education",
        "publishedDate":"2013",

这是我的java代码:

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

    try {

        JSONObject baseJsonResponse = new JSONObject(bookJSON);
        JSONArray bookArray = baseJsonResponse.getJSONArray("items");

        for (int i = 0; i <= bookArray.length(); i++) {

            JSONObject currentBook = bookArray.getJSONObject(i);
            JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");

            String bookTitle = volumeInfo.optString("title").toString();

            JSONArray authorArray = currentBook.getJSONArray("authors");

            for (int j =0; j <= authorArray.length(); j++) {

                String authorBook = authorArray.optString(0).toString(); // error
            }

            String publishedDate = volumeInfo.optString("publishedDate").toString();

            Book book = new Book(bookTitle, authorBook, publishedDate);
            bookLists.add(book);
        }

    } catch (JSONException e){

        Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
    }

    return bookLists;

3 个答案:

答案 0 :(得分:1)

您正在尝试从当前不存在的currentBook JSON对象中获取authors数组。您需要从volumeInfo JSON对象中获取authors数组。

然后,您需要在for循环的范围之外声明您的字符串变量authorBook。

JSONArray authorArray = volumeInfo.getJSONArray("authors");
String authorBook = "";
for (int i = 0; i < authorArray.length(); i++) {
    // Add a comma after each author unless it is the last one.
    String author = authorArray.getString(i);
    if (i == authorArray.length() - 1) {
       authorBook = authorBook + author;
    } else {
       authorBook = authorBook + author + ", ";
    }
}

您的变量authorBook应该是以逗号分隔的作者字符串。

答案 1 :(得分:0)

您可以添加一个List以将作者存储到Book类 然后解析应该看起来像

`

JSONArray authorArray = currentBook.getJSONArray("authors");
List<String> authors = new ArrayList<>();

for (int j =0; j <= arr.length(); j++) {
    String author = authorArray.getString(i);
    authors.add(author);
}
// When you create the book 

 Book book = new Book(bookTitle, authorBook, publishedDate, authors);
 bookLists.add(book);

`

答案 2 :(得分:0)

JSONArray authorArray = currentBook.getJSONArray("authors");
List<String> authors = new ArrayList<>();

for (int j =0; j <= arr.length(); j++) {
    String author = authorArray.getString(j);
    authors.add(author);
}
// When you create the book 

 Book book = new Book(bookTitle, authorBook, publishedDate, authors);
 bookLists.add(book);


or use forech();

`