JSON Beautifier Library for Java

后端 未结 4 1161
栀梦
栀梦 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:39

    Assuming you're starting out with an existing JSON string, then Jackson can do this for you:

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
    
    String originalJson = ...
    JsonNode tree = objectMapper .readTree(originalJson);
    String formattedJson = objectMapper.writeValueAsString(tree);
    

提交回复
热议问题