jackson将Bean,List,Map转为Json源码展示(小白,欢迎指导)

1.0首先创建一个user类

package com.itheima.josn;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

public class User {
    private String username;
    private String password;
    private Date birthday;
    private Integer age;

    public User() {
    }

    public User(String username, String password, Date birthday, Integer age) {

        this.username = username;
        this.password = password;
        this.birthday = birthday;
        this.age = age;
    }

    public String getUsername() {

        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    //@JsonFormat 默认的时区是Greenwich Time,和背景事件相差8小时,需要在注释里写上时区
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", age=" + age +
                '}';
    }
}
	注意事项:如果数据类型为Date的话,需要在对应的get/set方法上添加JsonFormat注解
	由于时区不一样(一般是差8个小时),所以需要通过手动添加时区 imezone = "GMT+8"

2.0然后创建一个测试类进行解析

package com.itheima.josn;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.util.*;

public class jsonTest {
    @Test
    public void jsonTest() throws JsonProcessingException {

        /*Bean转json*/
        User user = new User("jack","123",new Date(),18);
        //创建核心解析器
        ObjectMapper om = new ObjectMapper();
        //调用方法解析对象
        String json = om.writeValueAsString(user);//解析结果{username=''jack,password='123',birthday='',age='18'}
//        System.out.println(json);

        /*list集合转json*/
        ArrayList<User> userList = new ArrayList<>();
        userList.add(new User("jack","123",new Date(),18));
        userList.add(new User("tom","123",new Date(),19));
        userList.add(new User("andy","123",new Date(),20));
        //创建核心解析器
        ObjectMapper objectMapper = new ObjectMapper();
        //调用方法解析
        String arrayJosn = objectMapper.writeValueAsString(userList);//解析[{username='jack',password='123'...}{...}{...}]
//        System.out.println(arrayJosn);

        /*Map集合转json*/
        HashMap<String, String> map = new HashMap<>();
        map.put("username","jack");
        map.put("password","123");
        map.put("birthday","new Date()");
        map.put("age","18");
        //转换为set集合
        Set<Map.Entry<String, String>> setList = map.entrySet();
        ObjectMapper obj = new ObjectMapper();
        String mapJson = obj.writeValueAsString(setList);
        System.out.println(mapJson);//解析结果:[{"key":"birthday","value":"new Date()"},{...},{...},{...}]

    }
}

打印结果展示:
jackson将Bean,List,Map转为Json源码展示(小白,欢迎指导)

上一篇:一个简单的Webservice的demo(中)_前端页面调用


下一篇:codeforces590E Birthday【AC自动机+Floyd+匈牙利算法】