VO 를 json으로 변경
VO를 String으로 변경할 일이 생겼다.
VO --> JSON --> String으로 변경하는 방법을 선택하게 되었다.
1. com.fasterxml.jackson.core 라이브러리 등록
전자정부프레임워크에 기본적으로 들어있어서 따로 등록하지 않아도 되었다.
버전은 적당한 버전으로.
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
2. import
import com.fasterxml.jackson.databind.ObjectMapper;
3. VO를 JSON 형식의 문자열로 변경한다.
aaaVO aaa = new aaaVO():
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(aaa );
여기까지..
@ResponseBody
public String weatherToday(ModelMap model)throws Exception {
aaaVO aaa = new aaaVO():
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(aaa );
return jsonString;
}
@ResponseBody 를 써준다.
4. 자바에서 JSON형태로는 아래와 같이 받을 수 있다.
JSONParser jsonParser = new JSONParser();
JSONObject jsonObj = (JSONObject)jsonParser.parse(jsonString);