출처 - http://www.mkyong.com/java/how-to-enable-pretty-print-json-output-jackson/
It’s easy to enable pretty print JSON output in Jackson framework. Take last object to/from json example :
User user = new User(); ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writeValueAsString(user));
It will display JSON in compact mode :
{"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"mkyong"}
To enable pretty print, just call the "defaultPrettyPrintingWriter()
” like this :
User user = new User(); ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(user));
Output
{ "age" : 29, "messages" : [ "msg 1", "msg 2", "msg 3" ], "name" : "mkyong" }
See a full example.
package com.mkyong.core; import java.io.IOException; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; public class JacksonExample { public static void main(String[] args) { User user = new User(); ObjectMapper mapper = new ObjectMapper(); try { System.out.println(mapper.defaultPrettyPrintingWriter() .writeValueAsString(user)); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
'JAVA OPEN API' 카테고리의 다른 글
[maven] pom 설정 빌드시 jar파일 target 으로 링크 되는 오류 (0) | 2014.02.14 |
---|---|
[Junit-펌] @Parameters (0) | 2013.12.23 |
[mybatis] collection 의 resultMap Null 처리 -> JSON [] (0) | 2013.11.27 |
[ehcache]-[펌] 캐시 Ehcache (0) | 2013.10.23 |
[log4j] 로그파일위치변경 (0) | 2013.10.23 |