来自JsonArray的JsonArray

时间:2017-05-05 20:49:59

标签: java android arrays json json-api

想要从此链接中提取数据 - https://www.googleapis.com/books/v1/volumes?q=android&maxResults=16

这是我提取标题和作者的代码,但我可以获得标题而不是作者。

JSONObject root = new JSONObject(bookJSON);  
JSONArray bookArray = root.getJSONArray("items");
String autho = " ";

for (int i = 0; i < bookArray.length(); i++) {
    JSONObject currentBook = bookArray.getJSONObject(i);
    JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");

    String title = volumeInfo.getString("title");
    Log.i("booktitle",title);

    JSONArray auth = volumeInfo.getJSONArray("authors");
    for (int j = 0; j > auth.length(); j++) {
        autho = auth.getString(j);
        Log.i("authorname",autho);
    }
}

1 个答案:

答案 0 :(得分:0)

JSONArray auth = volumeInfo.getJSONArray("authors");
int len = auth.length();
for (int j = 0; j < len; j++) {
    autho = auth.get(j).toString();
    Log.i("authorname",autho);
}

您需要获取JSONArray的每个索引元素,然后将其转换为String