-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveJSON.java
More file actions
43 lines (36 loc) · 1.03 KB
/
RecursiveJSON.java
File metadata and controls
43 lines (36 loc) · 1.03 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
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
public class RecursiveJSON {
private Object root;
public RecursiveJSON() {
this.root = null;
}
public RecursiveJSON(Object root) {
this.root = root;
}
public RecursiveJSON getObject(String key) {
if (root == null) {
return new RecursiveJSON();
} else {
return new RecursiveJSON(((JSONObject) root).get(key));
}
}
public ArrayList<RecursiveJSON> getArray(String key) {
ArrayList<RecursiveJSON> arrayList = new ArrayList<>();
if (root != null) {
JSONArray arr = (JSONArray) ((JSONObject) root).get(key);
for (Object obj : arr) {
arrayList.add(new RecursiveJSON(obj));
}
}
return arrayList;
}
public Object getValue(String key) {
if (root == null) {
return null;
} else {
return ((JSONObject) root).get(key);
}
}
}