-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataReader.java
More file actions
55 lines (45 loc) · 1.63 KB
/
Copy pathDataReader.java
File metadata and controls
55 lines (45 loc) · 1.63 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package CSCE247_Library;
import java.io.FileReader;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class DataReader {
private static final String BOOK_LIST = "src/Data.json";
public static ArrayList<Book> loadItems() {
ArrayList<Book> item = new ArrayList<Book>();
try {
FileReader reader = new FileReader(BOOK_LIST);
JSONParser parser = new JSONParser();
JSONObject jsonData = (JSONObject)new JSONParser().parse(reader);
JSONArray itemJSON = (JSONArray)jsonData.get("item");
for(int i = 0; i < itemJSON.size(); i++) {
JSONObject itemsJSON = (JSONObject)itemJSON.get(i);
String title = (String)itemsJSON.get("title");
String author = (String)itemsJSON.get("author");
int year = (int)itemsJSON.get("year");
String desc = (String)itemsJSON.get("desc");
String borrower = (String)itemsJSON.get("borrower");
String series = (String)itemsJSON.get("serires");
double[] ratings = (double[])itemsJSON.get("ratings");
int pages = (int)itemsJSON.get("pages");
String awards = (String)itemsJSON.get("awards");
String publisher = (String)itemsJSON.get("publisher");
String edition = (String)itemsJSON.get("edition");
JSONArray genre = (JSONArray) jsonData.get("genre");
for (Object g : genre) {
System.out.println(g + "");
}
JSONArray type = (JSONArray) jsonData.get("type");
for (Object t : type) {
System.out.print(t + "");
}
item.add(new Book());
}
return item;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}