-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGsonStreamApiRead.java
42 lines (26 loc) · 1.29 KB
/
GsonStreamApiRead.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
31
32
33
34
35
36
37
38
39
40
41
42
package com.zetcode;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import java.io.IOException;
import java.io.StringReader;
public class GsonStreamApiRead {
public static void main(String[] args) throws IOException {
String json_string = "{\"name\":\"chair\",\"quantity\":3}";
try (JsonReader reader = new JsonReader(new StringReader(json_string))) {
while (reader.hasNext()) {
JsonToken nextToken = reader.peek();
if (JsonToken.BEGIN_OBJECT.equals(nextToken)) {
reader.beginObject();
} else if (JsonToken.NAME.equals(nextToken)) {
reader.nextName();
} else if (JsonToken.STRING.equals(nextToken)) {
String value = reader.nextString();
System.out.format("%s: ", value);
} else if (JsonToken.NUMBER.equals(nextToken)) {
long value = reader.nextLong();
System.out.println(value);
}
}
}
}
}