我正在尝试解析以下JSON数据以将书籍的作者和标题存储到ListView中:
{
"kind": "books#volumes",
"totalItems": 921,
"items": [{
"kind": "books#volume",
"id": "QP7VvnhDOOsC",
"etag": "BQzzyT9zza0",
"selfLink": "https://www.googleapis.com/books/v1/volumes/QP7VvnhDOOsC",
"volumeInfo": {
"title": "Programming Android",
"authors": ["Zigurd Mednieks", "Laird Dornin", "G. Blake Meike", "Masumi Nakamura"],
"publisher": "\"O'Reilly Media, Inc.\"",
"publishedDate": "2012",
"description": "Presents instructions for creating Android applications for mobile devices using Java.",
"industryIdentifiers": [{
"type": "ISBN_13",
"identifier": "9781449316648"
}, {
"type": "ISBN_10",
"identifier": "1449316646"
}],
"readingModes": {
"text": false,
"image": true
},
"pageCount": 542,
"printType": "BOOK",
"categories": ["Computers"],
"averageRating": 3.5,
"ratingsCount": 6,
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "preview-1.0.0",
"imageLinks": {
"smallThumbnail": "http://books.google.co.in/books/content?id=QP7VvnhDOOsC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.co.in/books/content?id=QP7VvnhDOOsC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.co.in/books?id=QP7VvnhDOOsC&printsec=frontcover&dq=android+intitle&hl=&cd=1&source=gbs_api",
"infoLink": "http://books.google.co.in/books?id=QP7VvnhDOOsC&dq=android+intitle&hl=&source=gbs_api",
"canonicalVolumeLink": "http://books.google.co.in/books/about/Programming_Android.html?hl=&id=QP7VvnhDOOsC"
},
"saleInfo": {
"country": "IN",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "IN",
"viewability": "PARTIAL",
"embeddable": true,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": false
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "http://books.google.co.in/books/reader?id=QP7VvnhDOOsC&hl=&printsec=frontcover&output=reader&source=gbs_api",
"accessViewStatus": "SAMPLE",
"quoteSharingAllowed": false
},
"searchInfo": {
"textSnippet": "Presents instructions for creating Android applications for mobile devices using Java."
}
}, {
"kind": "books#volume",
"id": "S1QQ-mOaPd8C",
"etag": "hbStdCg087k",
"selfLink": "https://www.googleapis.com/books/v1/volumes/S1QQ-mOaPd8C",
"volumeInfo": {
"title": "Sams Teach Yourself Java in 21 Days (Covering Java 7 and Android)",
"authors": ["Rogers Cadenhead"],
以下是我用于JSON解析并将数据添加到ArrayList的方法。
public void DisplayList(String result) {
try {
JSONObject jsonobject = new JSONObject(result);
JSONArray jarray = jsonobject.getJSONArray("items");
for (int i = 0; i < jarray.length(); i++) {
JSONObject volumeInfo = jarray.getJSONObject(i).getJSONObject("volumeInfo");
String book_title = volumeInfo.getString("title");
JSONArray authors = volumeInfo.getJSONArray("authors");
for (int j = 0; j < authors.length(); j++) {
String book_author = authors.getString(i);
books.add(new Book(book_title, book_author));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
BooksAdapter adapter = new BooksAdapter(this, books);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
//listView.setVisibility((adapter.isEmpty())?View.GONE:View.VISIBLE);
if (adapter.isEmpty()) {
defaultTextView.setText(no_response_message[2]);
//"No data to display, please enter correct keyword and search again."
}
}
当我使用函数books.add()时,它运行得很好,但是当我尝试显示ListView时,我弹出了ListView空消息。
由于我无法将数据存储到ArrayList中,可能是什么错误。
答案 0 :(得分:1)
首先,JSON字符串没有正确关闭,它应该是:
{"kind":"books#volumes","totalItems":921,"items":[{"kind":"books#volume","id":"QP7VvnhDOOsC","etag":"BQzzyT9zza0","selfLink":"https://www.googleapis.com/books/v1/volumes/QP7VvnhDOOsC","volumeInfo":{"title":"Programming Android","authors":["Zigurd Mednieks","Laird Dornin","G. Blake Meike","Masumi Nakamura"],"publisher":"\"O'Reilly Media, Inc.\"","publishedDate":"2012","description":"Presents instructions for creating Android applications for mobile devices using Java.","industryIdentifiers":[{"type":"ISBN_13","identifier":"9781449316648"},{"type":"ISBN_10","identifier":"1449316646"}],"readingModes":{"text":false,"image":true},"pageCount":542,"printType":"BOOK","categories":["Computers"],"averageRating":3.5,"ratingsCount":6,"maturityRating":"NOT_MATURE","allowAnonLogging":false,"contentVersion":"preview-1.0.0","imageLinks":{"smallThumbnail":"http://books.google.co.in/books/content?id=QP7VvnhDOOsC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api","thumbnail":"http://books.google.co.in/books/content?id=QP7VvnhDOOsC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"},"language":"en","previewLink":"http://books.google.co.in/books?id=QP7VvnhDOOsC&printsec=frontcover&dq=android+intitle&hl=&cd=1&source=gbs_api","infoLink":"http://books.google.co.in/books?id=QP7VvnhDOOsC&dq=android+intitle&hl=&source=gbs_api","canonicalVolumeLink":"http://books.google.co.in/books/about/Programming_Android.html?hl=&id=QP7VvnhDOOsC"},"saleInfo":{"country":"IN","saleability":"NOT_FOR_SALE","isEbook":false},"accessInfo":{"country":"IN","viewability":"PARTIAL","embeddable":true,"publicDomain":false,"textToSpeechPermission":"ALLOWED","epub":{"isAvailable":false},"pdf":{"isAvailable":false},"webReaderLink":"http://books.google.co.in/books/reader?id=QP7VvnhDOOsC&hl=&printsec=frontcover&output=reader&source=gbs_api","accessViewStatus":"SAMPLE","quoteSharingAllowed":false},"searchInfo":{"textSnippet":"Presents instructions for creating Android applications for mobile devices using Java."}},{"kind":"books#volume","id":"S1QQ-mOaPd8C","etag":"hbStdCg087k","selfLink":"https://www.googleapis.com/books/v1/volumes/S1QQ-mOaPd8C","volumeInfo":{"title":"Sams Teach Yourself Java in 21 Days (Covering Java 7 and Android)","authors":["Rogers Cadenhead"]}}]}
现在,您的解析功能正常工作,您的嵌套for中只有一个小错误,它应该是authors.getString(j)
而不是authors.getString(i)
。 for循环最终会成为:
for (int i = 0; i < jarray.length(); i++) {
JSONObject volumeInfo = jarray.getJSONObject(i).getJSONObject("volumeInfo");
String book_title = volumeInfo.getString("title");
JSONArray authors = volumeInfo.getJSONArray("authors");
for (int j = 0; j < authors.length(); j++) {
String book_author = authors.getString(j);
books.add(new Book(book_title, book_author));
}
}
无论如何,你仍然应该在ListView中显示一些内容,所以我猜你的适配器代码或Book类中还有另一个错误。