728x90
자바에서 JSON 파싱 및 변환을 위하여 많이 사용하는 Gson과 Jackson(ObjectMapper)에서 JSON 문자열을 출력할 때, 이쁘게 출력(pretty printing)하는 간단한 방법을 소개한다.
Gson
MyObject myObject = new MyObject();
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
String jsonString = gson.toJson(myObject);
Jackson(ObjectMapper)
MyObject myObject = new MyObject();
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(myObject);
반응형
'IT > PROGRAMMING' 카테고리의 다른 글
[Spring] spring-vault에서 @VaultPropertySource 여러개 사용하면 안되는 이유 (1) | 2019.04.19 |
---|---|
YouTube(유튜브) 동영상의 썸네일 이미지 추출하는 방법 (0) | 2019.04.09 |
[Spring] ConcurrentKafkaListenerContainerFactoryConfigurer를 사용하고 싶다. (0) | 2019.03.14 |
[Spring] Hibernate에서 "Could not build ClassFile" 발생하는 오류 해결 (0) | 2019.03.10 |
[Spring] spring-boot 1.5.X 부터 spring-kafka 사용할 수가 있네요. (0) | 2019.03.06 |