Gson
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。
可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。
示例代码:
get Json from Object
Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};
(Serialization)
gson.toJson(ints); ==> prints [1,2,3,4,5]
gson.toJson(strings); ==> prints ["abc", "def", "ghi"]
get Obect from Json
@Cacheable(value = "eems-roles-tree-cache", unless = "#result == null")
public List<EemsRole> listAllRoles() throws EndpointFailedException {
String dynamicRolesStr = eemsService.listAllRolesViaAdminAPI();// TODO: validate response , null check
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<EemsRole>>() {
}.getType();
List<EemsRole> allRoles = gson.fromJson(dynamicRolesStr, type);
return allRoles;
}