【数据格式】Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据

28 篇文章 467 订阅 ¥9.90 ¥99.00
本文介绍了如何使用Jackson库进行JSON数据的美化输出,包括对象和字符串两种情况。通过设置writerWithDefaultPrettyPrinter(),可以实现易读的格式化JSON,对于大型JSON数据尤为有用。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1.概述

转载:https://www.sojson.com/blog/245.html

Jackson 格式化输出JSON 代码说明(对象)
我们一般输出就是普通的toString 输出。如下代码:

Demo demo = new Demo("sojson",4,"https://www.sojson.com");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(demo));

输出结果:

{"name":"sojson","age":4,"domain":"https://www.sojson.com"}

非常不利于肉眼观看,而且一大坨,如果是JSON很大的话,那么更难受。那么我们美化输出呢?

美化/优雅/格式化输出,代码如下:

public static void main(String[] args) throws JsonProcessingException {
    Demo demo = new Demo("sojson",4,"https://www.sojson.com");
    ObjectMapper mapper = new ObjectMapper();
    //普通输出
    System.out.println(mapper.writeValueAsString(demo));
    //格式化/美化/优雅的输出
    System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(demo));
}

输出结果:

{
  "name" : "sojson",
  "age" : 4,
  "domain" : "https://www.sojson.com"
}

是不是结果很nice?下面再看下字符串输出。

Jackson 格式化输出JSON 代码说明(字符)
其实这里就是把字符串转成对象(Object ),然后再输出的。

优雅输出 Java代码:

public static void main(String[] args) throws IOException {
   //已知一个json 字符串
    String json = "{\"name\":\"sojson\",\"age\":4,\"domain\":\"https://www.sojson.com\"}";
    //求优雅输出
    ObjectMapper mapper = new ObjectMapper();
    Object obj = mapper.readValue(json, Object.class);
    System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj));
}

输出结果:

{
  "name" : "sojson",
  "age" : 4,
  "domain" : "https://www.sojson.com"
}

这里有的同学是不是想到,如果直接用上面的方法(writerWithDefaultPrettyPrinter() )呢,因为参数类型是Object ,其实我看了源码,Object 是为了你方便传参为各种你的对象。如果你传的String ,那么直接出来String 了。也就是还是输出原来的方式。当然你也可以试试。

Jackson Maven引入:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.4</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.4</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.4</version>
</dependency>

测试类请在附件中下载。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用SpringBoot框架时,可以通过添加Jackson依赖来实现JSON数据格式化。Jackson是一个流行的Java库,用于将Java对象转换为JSON格式的数据,也可以将JSON格式的数据转换为Java对象。 下面是实现JSON数据格式化的步骤: 1. 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> ``` 2. 在SpringBoot的配置类中添加以下代码: ```java @Configuration public class JacksonConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); return objectMapper; } @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(objectMapper()); return converter; } } ``` 3. 在Controller类中使用`@RestController`注解,并返回Java对象即可自动转换为JSON格式的数据。 ```java @RestController @RequestMapping("/user") public class UserController { @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { User user = new User(); user.setId(id); user.setUsername("test"); user.setPassword("123456"); return user; } } ``` 上述代码中,`User`类是一个Java对象,通过`@RestController`注解将其转换为控制器类。`getUserById`方法返回一个`User`对象,SpringBoot会自动将其转换为JSON格式的数据。 这样就完成了JSON数据格式化的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值