웹 개발

Convert a Map to a POJO

노루아부지 2020. 2. 26. 20:01
반응형

Method 1. Use "org.codehaus.jackson.map.ObjectMapper"

import org.codehaus.jackson.map.ObjectMapper;

Map<String, Object> map = new HashMap<>();
map.put("userId", "admin");
map.put("userName", "administrator");

ObjectMapper mapper = new ObjectMapper();
UserVO user = mapper.convertValue(expectedMap, UserVO.class);

 

Method 2. Use "com.google.gson.Gson"

import com.google.gson.Gson;
import com.google.gson.JsonElement;

Map<String, Object> map = new HashMap<>();
map.put("userId", "admin");
map.put("userName", "administrator");

Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree(expectedMap);
UserVO user = gson.fromJson(jsonElement, UserVO.class);
728x90
반응형
loading