-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonToObject.java
81 lines (60 loc) · 1.83 KB
/
JsonToObject.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
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
//package hahahahahahahaha
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
public class JsonToObject {
public static void main(String[] args) {
try {
// Json 문자열이 파일인 경우
// {
// "name": "Proxy-1"
// "routePath": [
// {"/notice" : "Proxy-2"},
// {"/service" : "Proxy-3"},
// {"/auth" : "Service-1" }
// ]
// }
Path path = new File(System.getProperty("user.dir") + File.separator + "json-parse-test.txt").toPath();
List<String> lines = Files.readAllLines(path);
// 또는 걍
String jsonStr = "{\"name\":\"Proxy-1\", \"routePath\":[{\"/notice\":\"Proxy-2\"},{\"/service\":\"Proxy-3\"},{\"/auth\":\"Service-1\"}]}";
// line by line 또는
for(String line : lines) {
// System.out.println(line);
}
System.out.println("================");
// jdk 11 미만은 이래야함
String contents = String.join("\n", lines);
// Gson 이용하여 모델로 변경
Gson gson = new Gson();
ProxyServer proxyServer = gson.fromJson(jsonStr, ProxyServer.class);
System.out.println(proxyServer.getName());
List<Map<String, String>> paths = proxyServer.getRoutePath();
for(Map pathMap : paths) {
System.out.println(pathMap.toString());
}
}catch(Exception e){
e.printStackTrace();
}
}
// Model Example
public class ProxyServer {
private String name;
private List<Map<String, String>> routePath;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Map<String, String>> getRoutePath() {
return routePath;
}
public void setRoutePath(List<Map<String, String>> routePath) {
this.routePath = routePath;
}
}
}