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

在Java中解析JSON时出现问题?,java,android,json,Java,Android,Json,我有一个带有3个文本视图的列表视图,它将显示图书列表API中的图书标题、作者和出版日期 现在这里的问题是我可以显示图书的标题和日期,但是authors元素在JSONArray中,为此,我考虑运行一个内部for循环,将作者添加到string变量中,但由于string变量位于内部循环中,因此当我尝试将变量传递给Book自定义类时,它是不可见的 我可以在“volumeInfo”JSONObject中显示字符串,问题出在“author”JSONArray上 很抱歉没有解释清楚,但如果有任何帮助,我们将不

我有一个带有3个
文本视图的
列表视图
,它将显示图书列表API中的图书标题、作者和出版日期

现在这里的问题是我可以显示图书的标题和日期,但是authors元素在
JSONArray
中,为此,我考虑运行一个内部for循环,将作者添加到string变量中,但由于string变量位于内部循环中,因此当我尝试将变量传递给Book自定义类时,它是不可见的

我可以在“
volumeInfo
JSONObject
中显示
字符串
,问题出在“author”
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;
List bookLists=new ArrayList();
试一试{
JSONObject baseJsonResponse=新的JSONObject(bookJSON);
JSONArray bookArray=baseJsonResponse.getJSONArray(“items”);

对于(int i=0;i您试图从不存在的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 + ", ";
    }
}
JSONArray authorary=volumeInfo.getJSONArray(“作者”);
字符串authorBook=“”;
for(int i=0;i

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

您可以添加一个to列表来将作者存储到Book类中 那么你应该是这样的

`

JSONArray authorary=currentBook.getJSONArray(“作者”);
列表作者=新建ArrayList();
对于(intj=0;j
JSONArray authorArray=currentBook.getJSONArray(“作者”);
列表作者=新建ArrayList();

对于(int j=0;j
String authorBook;/*对于分配值的循环*/
,这就是问题所在,在内部循环内声明或初始化的任何变量在其外部都不可见,如果在其外部声明该变量,则会得到一个“变量未初始化”内部for循环中出现错误。编辑答案以反映正确初始化authorBook变量。我收到一个JSON解析错误,当我完全注释掉authorBook填充为空字符串或变量中硬编码的任何字符串的内部for循环时,authorBook在for循环中未被解析或不可见在它之外。在for循环之前将authorBook初始化为空字符串,在for循环中迭代authorArray。如果注释掉该循环,则authorBook将是初始化它的空字符串。在我的示例中的for循环中,您只是将各个作者附加到空字符串中,因此它看起来像“Author A、Author B等”是的,我确实像你说的那样初始化了,但是我仍然得到一个JSON解析错误,当我注释掉内部for循环和应用程序工作的作者的JSON数组时,authorBooks没有正确解析。请记住,volumeInfo JSON对象中的书名和发布日期在listview中正确填充。我有一个自定义类这需要3个字符串变量并在自定义listview中填充它,我尝试像您所说的那样在内部循环中填充Arraylist,然后将Arraylist中的字符串放在循环后面的另一个变量中,并将字符串放在自定义类中,但仍然出现了解析错误。
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);
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();