-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONUtil.java
51 lines (39 loc) · 1.41 KB
/
JSONUtil.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
43
44
45
46
47
48
49
50
51
package javaToolkit.lib.utils;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JSONUtil {
public static JSONObject parseJSONFromFile(String jsonFpath) {
FileReader reader;
JSONObject object = null;
try {
reader = new FileReader(jsonFpath);
JSONParser jsonParser = new JSONParser();
object = (JSONObject) jsonParser.parse(reader);
} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
public static void writeMapAsJson(Map<String, Integer> map, String jsonFilePath) {
Map<String, String> newMap = new HashMap<String, String>();
for (String key : map.keySet()) {
newMap.put(key, String.valueOf(map.get(key)));
}
ObjectMapper mapper = new ObjectMapper();
String json = null;
try {
json = mapper.writeValueAsString(newMap);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
FileUtil.writeStr2File(json, jsonFilePath);
}
}