JSON Beautifier Library for Java

后端 未结 4 1163
栀梦
栀梦 2021-02-19 22:02

I want to format a string containing JSON data using Java. Does anybody know an open source library for that.

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-19 22:37

    With Jackson 2.6.1

    String beautify(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Object obj = mapper.readValue(json, Object.class);
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
    }
    

    pom.xml:

        
            com.fasterxml.jackson.core
            jackson-core
            2.6.1
        
    
        
            com.fasterxml.jackson.core
            jackson-databind
            2.6.1
        
    

    Convert JSON String to Pretty Print JSON output using Jackson

提交回复
热议问题