用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 }

 

转载于:https://www.cnblogs.com/yasepix/p/10009220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值