Spring Boot中操作JSON

本文介绍常用的JSON相关操作,Spring Boot内置Jackson包可以进行JSON相关操作

序列化

Fastjson
        User user1 = new User();
        String s1 = JSON.toJSONString(user1);
        System.out.println(s1);
Jackson
        ObjectMapper objectMapper = new ObjectMapper();
        String s2 = objectMapper.writeValueAsString(user);
        System.out.println(s2);

反序列化

Fastjson
        String s ="{\n" +
                "  \"id\": 1,\n" +
                "  \"userName\": \"zhangsan\",\n" +
                "  \"password\": \"123456\",\n" +
                "  \"userSex\": \"man\",\n" +
                "  \"nickName\": \"asdf\",\n" +
                "  \"birthday\": \"2000-09-11 00:00:00\"\n" +
                "}";
        User user = JSON.parseObject(s, User.class);
        System.out.println(user.toString());
Jackson
        User user = (User) objectMapper.readValue(s, User.class);
        System.out.println(user.toString());

自定义ObjectMapper

Spring Boot内置了Jackson来完成JSON的序列化和反序列化。

在Spring中使用@ResponseBody注解可以将方法返回的对象序列化成json串

在Spring Boot中可以自定义一个ObjectMapper来序列化我们想要返回的格式,比如序列化时间

package com.springboot.demos.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;

/**
 * @author zousy
 * @version v1.0
 * @Description
 * @date 2021-09-07 15:22
 */
@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper getObjectMapper(){
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return mapper;
    }
}

这样就会返回如下json串:

{
  "id": 1,
  "userName": "zhangsan",
  "password": "123456",
  "nickName": "asdf",
  "birthday": "2000-09-11 00:00:00",
  "sex": "man"
}

Jackson注解

1.@JsonProperty

@JsonProperty,作用在属性上,用来为JSON Key指定一个别名。

2.@Jsonlgnore

@Jsonlgnore,作用在属性上,用来忽略此属性。

3.@JsonIgnoreProperties

@JsonIgnoreProperties,忽略一组属性,作用于类上,比如JsonIgnoreProperties({ “password”, “birthday” })。

4.@JsonFormat

@JsonFormat,用于日期格式化,如:@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”)

还有其他的一些注解,这里不再介绍可参考官方文档。

JSON相关操作

JSON数组字符串–>List

Jackson

    String jsonArray = "[{\"brand\":\"ford\"}, {\"brand\":\"Fiat\"}]";

 	ObjectMapper objectMapper = new ObjectMapper();

 	List<Car> cars1 = objectMapper.readValue(jsonArray, new TypeReference<List<Car>>(){});

Fastjson

        String jsonArray = "[{\"brand\":\"ford\"}, {\"brand\":\"Fiat\"}]";

        List<Car> cars = JSON.parseArray(jsonArray, Car.class);

Jackson 主要操作JSON的类是 ObjectMapper
FastJson 主要操作JSON的类是 JSON、JSONObject、JSONArray

参考

  1. Spring Boot中的JSON技术
  2. Jackson使用详解
  3. FastJson使用详解
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值