zoukankan      html  css  js  c++  java
  • 用jackson输出标准的json字符串

     1 public class JacksonObjectMapperExample {
     2 
     3     public static String formatJson(String jsonStr) throws IOException {
     4         ObjectMapper objectMapper = new ObjectMapper();
     5         // 允许没有引号的字段名(非标准)
     6         objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
     7         // 允许单引号(非标准)
     8         objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
     9         Object json = objectMapper.readValue(jsonStr, Object.class);
    10         //美化
    11         //System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
    12         //objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    13         //不美化
    14         objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
    15 
    16         return objectMapper.writeValueAsString(json);
    17     }
    18 
    19     public static void main(String[] args) throws IOException {
    20         String test = "{age:29,messages:["msg 1","msg 2","msg 3"],"name":"mkyong"}";
    21         System.out.println(formatJson(test));
    22     }
    23 }
  • 相关阅读:
    CF1580B Mathematics Curriculum
    [机房测试]变异大老鼠
    http_缓存
    UDP_概述
    记录: webAssembly 延申
    Event
    NetWork_timeLine
    基于Typora的Latex代码书写并移植到word中
    ZooKeeper学习总结
    HBase学习总结
  • 原文地址:https://www.cnblogs.com/yasepix/p/10009220.html
Copyright © 2011-2022 走看看