-
Notifications
You must be signed in to change notification settings - Fork 5
/
GsonExample.java
36 lines (34 loc) · 1.21 KB
/
GsonExample.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
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class GsonExample {
public static String text = "{\n"
+ " \"cis\": [\n"
+ " {\n"
+ " \"name\": \"CIS120\",\n"
+ " \"instructor\": \"Dr. Weirich\",\n"
+ " \"languages\": [\"OCaml\", \"Java\"]\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"CIS121\",\n"
+ " \"instructor\": \"Dr. Tannen\",\n"
+ " \"languages\": [\"Java\"]\n"
+ " }\n"
+ " ]\n"
+ "}\n";
public static void main(String args[]) {
JsonParser parser = new JsonParser();
JsonObject o = parser.parse(text).getAsJsonObject();
for (JsonElement e : o.get("cis").getAsJsonArray()) {
JsonObject course = e.getAsJsonObject();
System.out.println("Course name: " + course.get("name").getAsString() + "\n");
System.out.println("Instructor: " +
course.get("instructor").getAsString() + "\n");
System.out.println("Languages taught:\n");
for (JsonElement lang : course.get("languages").getAsJsonArray()) {
System.out.println(" - " + lang.getAsString() + "\n");
}
}
}
}