-
Notifications
You must be signed in to change notification settings - Fork 1
/
BestSellersJSON.java
30 lines (26 loc) · 1.09 KB
/
BestSellersJSON.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package edu.virginia.cs.threelayer.data;
import edu.virginia.cs.threelayer.BestSellersList;
import edu.virginia.cs.threelayer.Book;
import org.json.JSONArray;
import org.json.JSONObject;
public class BestSellersJSON {
public BestSellersList getBestSellerList(JSONObject jsonObject) {
BestSellersList bestSellersList = new BestSellersList();
JSONArray bookList = jsonObject.getJSONObject("results").getJSONArray("books");
for (Object object : bookList) {
if (object instanceof JSONObject bookJson) {
Book book = getBookFromJSON(bookJson);
int rank = bookJson.getInt("rank");
bestSellersList.addBook(book, rank);
}
}
return bestSellersList;
}
public Book getBookFromJSON(JSONObject bookJson) {
String isbn = bookJson.getString("primary_isbn10");
String title = bookJson.getString("title");
String author = bookJson.getString("author");
int weeksOnList = bookJson.getInt("weeks_on_list");
return new Book(isbn, title, author, weeksOnList);
}
}