我正在从json文件中读取数据,并且我想按类别获取项目
public List<Item> getItemsByCategory(String category) {
List<Item> matchingCategory = new ArrayList<>();
for (Item item : books.getItems()) {
List<String> categories1 = item.getVolumeInfo().getCategories();
// categories1 == null for the first item so the below for loop fails
for (String cat : categories1) {
if (cat.equals(category)) {
matchingCategory.add(item);
} else throw new MyResourceNotFoundException(String.format("category with type %s is not found"));
}
}
return matchingCategory;
}
我在json文件中的第一个项目没有类别,这就是为什么在第一个循环中category为null,我如何跳过它并继续在json文件中的项目中,而不是获得null指针异常
我尝试使用break,但是它结束了循环而没有开始下一个
这是我的json我从这里读取数据。
{
"requestedUrl": "https://www.googleapis.com/books/v1/volumes?q=java&maxResults=40",
"items": [
{
"kind": "books#volume",
"id": "7tkN1CYzn2cC",
"etag": "pfjjxSpetIM",
"selfLink": "https://www.googleapis.com/books/v1/volumes/7tkN1CYzn2cC",
"volumeInfo": {
"title": "A Hypervista of the Java Landscape",
"publisher": "InfoStrategist.com",
"industryIdentifiers": [
{
"type": "ISBN_13",
"identifier": "9781592432172"
},
{
"type": "ISBN_10",
"identifier": "1592432174"
}
],
"readingModes": {
"text": true,
"image": true
},
"printType": "BOOK",
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "1.0.1.0.preview.3",
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=7tkN1CYzn2cC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=7tkN1CYzn2cC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.pl/books?id=7tkN1CYzn2cC&pg=PP1&dq=java&hl=&cd=1&source=gbs_api",
"infoLink": "http://books.google.pl/books?id=7tkN1CYzn2cC&dq=java&hl=&source=gbs_api",
"canonicalVolumeLink": "https://books.google.com/books/about/A_Hypervista_of_the_Java_Landscape.html?hl=&id=7tkN1CYzn2cC"
},
"saleInfo": {
"country": "PL",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "PL",
"viewability": "PARTIAL",
"embeddable": true,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": true,
"acsTokenLink": "http://books.google.pl/books/download/A_Hypervista_of_the_Java_Landscape-sample-epub.acsm?id=7tkN1CYzn2cC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
},
"pdf": {
"isAvailable": true,
"acsTokenLink": "http://books.google.pl/books/download/A_Hypervista_of_the_Java_Landscape-sample-pdf.acsm?id=7tkN1CYzn2cC&format=pdf&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
},
"webReaderLink": "http://play.google.com/books/reader?id=7tkN1CYzn2cC&hl=&printsec=frontcover&source=gbs_api",
"accessViewStatus": "SAMPLE",
"quoteSharingAllowed": false
}
},
{
"kind": "books#volume",
"id": "-SYM4PW-YAgC",
"etag": "dXytGSDckJk",
"selfLink": "https://www.googleapis.com/books/v1/volumes/-SYM4PW-YAgC",
"volumeInfo": {
"title": "The Religion of Java",
"authors": [
"Clifford Geertz"
],
"publisher": "University of Chicago Press",
"publishedDate": "1976-02-15",
"description": "Written with a rare combination of analysis and speculation, this comprehensive study of Javanese religion is one of the few books on the religion of a non-Western people which emphasizes variation and conflict in belief as well as similarity and harmony. The reader becomes aware of the intricacy and depth of Javanese spiritual life and the problems of political and social integration reflected in the religion. The Religion of Java will interest specialists in Southeast Asia, anthropologists and sociologists concerned with the social analysis of religious belief and ideology, students of comparative religion, and civil servants dealing with governmental policy toward Indonesia and Southeast Asia.",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "0226285103"
},
{
"type": "ISBN_13",
"identifier": "9780226285108"
}
],
"readingModes": {
"text": true,
"image": true
},
"pageCount": 392,
"printType": "BOOK",
"categories": [
"Religion"
],
"averageRating": 4.0,
"ratingsCount": 4,
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "2.1.2.0.preview.3",
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=-SYM4PW-YAgC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=-SYM4PW-YAgC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.pl/books?id=-SYM4PW-YAgC&printsec=frontcover&dq=java&hl=&cd=2&source=gbs_api",
"infoLink": "http://books.google.pl/books?id=-SYM4PW-YAgC&dq=java&hl=&source=gbs_api",
"canonicalVolumeLink": "https://books.google.com/books/about/The_Religion_of_Java.html?hl=&id=-SYM4PW-YAgC"
},
"saleInfo": {
"country": "PL",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "PL",
"viewability": "PARTIAL",
"embeddable": true,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": true,
"acsTokenLink": "http://books.google.pl/books/download/The_Religion_of_Java-sample-epub.acsm?id=-SYM4PW-YAgC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "http://play.google.com/books/reader?id=-SYM4PW-YAgC&hl=&printsec=frontcover&source=gbs_api",
"accessViewStatus": "SAMPLE",
"quoteSharingAllowed": false
},
"searchInfo": {
"textSnippet": "Written with a rare combination of analysis and speculation, this comprehensive study of Javanese religion is one of the few books on the religion of a non-Western people which emphasizes variation and conflict in belief as well as ..."
}
} etc
如上所述,第一个项目没有类别,但是第二个类别,所以我应该能够获得第二个类别
答案 0 :(得分:1)
这是我所做的,还使用了continue和break,但是我必须删除我的异常处理程序,
public List<Item> getItemsByCategory(String category) {
List<Item> matchingCategory = new ArrayList<>();
for (Item item : books.getItems()) {
List<String> categories1 = item.getVolumeInfo().getCategories();
if(categories1 == null) continue;
for (String cat : categories1) {
if (cat.equals(category)) {
matchingCategory.add(item);
} else break; //throw new MyResourceNotFoundException(String.format("category with type %s is not found", category));
}
}
return matchingCategory;
}
如何退回引发新异常
答案 1 :(得分:0)
为什么不能添加条件??
if(categories1 != null) {
for() {
...
}
}