-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonParseRecursive.java
More file actions
85 lines (74 loc) · 2.22 KB
/
JsonParseRecursive.java
File metadata and controls
85 lines (74 loc) · 2.22 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.api.utils;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class JsonParseRecursive {
public static HashMap<String, Object> setKeys(String str,
ArrayList<String> keys) {
HashMap<String, Object> toReturn = new HashMap<String, Object>();
for (String key : keys) {
toReturn.put(key, null);
}
JSONObject obj = (JSONObject) JSONValue.parse(str);
setKeysR(obj, toReturn);
return toReturn;
}
public static HashMap<String, Object> setKeys(String str,
String[] keys) {
HashMap<String, Object> toReturn = new HashMap<String, Object>();
for (String key : keys) {
toReturn.put(key, null);
}
JSONObject obj = (JSONObject) JSONValue.parse(str);
setKeysR(obj, toReturn);
return toReturn;
}
private static void setKeysR(JSONObject obj, HashMap<String, Object> map) {
if (obj.keySet().size() > 0) {
for (String key : map.keySet()) {
if (obj.containsKey(key)) {
map.put(key, obj.get(key));
System.out.println("Found! -->" + obj.get(key).toString());
}
}
for (Object keyT : obj.keySet()) {
if (obj.get(keyT) instanceof JSONArray) {
for (Object objT : (JSONArray) obj.get(keyT)) {
if (objT instanceof JSONObject) {
setKeysR((JSONObject) objT, map);
}
}
}
if (obj.get(keyT) instanceof JSONObject) {
setKeysR((JSONObject) obj.get(keyT), map);
}
}
}
return;
}
public static HashMap<String, Object> getMap(String str) {
HashMap<String, Object> toReturn = new HashMap<String, Object>();
JSONObject obj = (JSONObject) JSONValue.parse(str);
getKeysR(obj, toReturn);
return toReturn;
}
private static void getKeysR(JSONObject obj, HashMap<String, Object> map) {
if (obj!=null && obj.keySet().size() > 0) {
for (Object keyT : obj.keySet()) {
map.put(keyT.toString(), obj.get(keyT));
if (obj.get(keyT) instanceof JSONArray) {
for (Object objT : (JSONArray) obj.get(keyT)) {
if (objT instanceof JSONObject) {
getKeysR((JSONObject) objT, map);
}
}
} else if (obj.get(keyT) instanceof JSONObject) {
getKeysR((JSONObject) obj.get(keyT), map);
}
}
}
return;
}
}