-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGsonReadList.java
More file actions
36 lines (26 loc) · 884 Bytes
/
GsonReadList.java
File metadata and controls
36 lines (26 loc) · 884 Bytes
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
package com.zetcode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
class Item {
private String name;
private int quantity;
@Override
public String toString() {
return "Item{" + "name=" + name + ", quantity=" + quantity + '}';
}
}
public class GsonReadList {
public static void main(String[] args) throws IOException {
Gson gson = new GsonBuilder().create();
try (Reader reader = new FileReader("src/main/resources/items.json")) {
List<Item> items = gson.fromJson(reader,
new TypeToken<List<Item>>(){}.getType());
items.forEach(System.out::println);
}
}
}