Frontend/JSON
ObjectMapper, JSONObject
지구
2018. 5. 11. 12:17
ObjectMapper
Domain Object와 JSON객체를 서로 변환시켜주는 라이브러리,
이 기능을 사용하려면 org.codehaus.jackson.map.ObjectMapper 라이브러리가 import 되어있어야 한다.
.writeValueAsString (VO->JSON)
1 2 3 4 5 | User user = new User("user01", "홍길동", 20); String json = new objectMapper().writeValueAsString(user); System.out.println(json); | cs |
.readValue (JSON->VO)
1 2 3 | User returnUser = objectMapper.readValue(json, User.class); System.out.println(returnUser); | cs |
JSONObject
JSON Data를 읽을 수 있으며, .get 메소드를 활용하여 특정 Field 값을 추출할 수 있다.
1 2 3 | JSONObject jsonObj = (JSONObject)JSONValue.parse(json); System.out.println(jsonObj); | cs |
반응형