-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONParser.java
More file actions
38 lines (32 loc) · 1 KB
/
Copy pathJSONParser.java
File metadata and controls
38 lines (32 loc) · 1 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
package com.core.utils;
import com.core.entity.Employee;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
* @author ManhKM on 6/21/2022
* @project Java-Utils
*/
public class JSONParser {
private static final GsonBuilder gsonBuilder = new GsonBuilder();
private static final Gson gson = gsonBuilder.create();
public static Object fromJson(String o, Class<?> c) {
try {
return gson.fromJson(o, c);
} catch (Exception e) {
return null;
}
}
public static String toJson(Object o) {
try {
return gson.toJson(o);
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
// Employee employee = new Employee("ManhKM", 18, "ThaiBinh");
// System.out.println(toJson(employee));
String dataJson = "{\"name\":\"TrangTT\",\"age\":18,\"address\":\"HaNam\"}";
System.out.println(fromJson(dataJson, Employee.class).toString());
}
}