본문 바로가기
Frontend/JSON

ObjectMapper, JSONObject

by 지구 2018. 5. 11.

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


반응형

'Frontend > JSON' 카테고리의 다른 글

JSON 과 String 의 그 사이..  (0) 2018.05.17
@RequestBody  (0) 2018.05.11
JSON 기본 정리  (0) 2018.05.10

댓글