2016-08-24 86 views
-1

我有一個ListView和3 TextView,它將顯示圖書標題,作者和來自圖書清單API的發佈日期。在Java中解析JSON時出現問題?

現在,這裏的問題是,我可以顯示書籍的標題和日期,但作者元件爲JSONArray,對於我在想運行一個內部的循環,將增加作者的字符串變量中,但由於串變量是在內部循環內,當我嘗試將變量傳遞給Book自定義類時,它不可見。

我可以在「volumeInfoJSONObject之內顯示String,這是「作者」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; 
+0

'String authorBook;/* for循環分配值* /' – vlaz

回答

1

你正在試圖獲得來自currentBook JSON對象,其中不存在作者陣列。您需要從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應該是由逗號分隔的作者字符串。

+0

這就是問題所在,在內部循環中聲明或初始化的任何變量在其外部都是不可見的,如果我在外部聲明瞭該變量,則會得到一個「變量未初始化」的錯誤內循環。 – Athelon

+0

編輯答案反映正確初始化authorBook變量。 –

+0

我得到了一個JSON解析錯誤,當我完全註釋掉內部for循環時,authorBook填充爲空字符串或任何在變量內硬編碼的字符串,authorBook沒有在for循環內被解析,或者它在外部不可見。 – Athelon

0

您可以添加到列表中,作者存儲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); 

`

+0

我有一個自定義類,需要3個字符串變量,並填充它在自定義列表視圖,我試圖填充內部循環內的Arraylist像你說的,然後把來自arraylist中的字符串在循環後的另一個變量和自定義類中的字符串,但我仍然有一個解析錯誤。 – Athelon

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

`