fastjson 序列化反序列化实践整理

只需要这一个就行


    com.alibaba
    fastjson
    ${fastjson.version}

user类

import com.alibaba.fastjson.annotation.JSONField;
public class User{
 
    private Integer id;
    private String userName;
    private Integer age;
    private Boolean isFalse;
    private String birthday;

//也可以自己封装日期,但是需要一个String字段

@JSONField(format = "yyyy-MM-dd HH:mm:ss")//序列化日期,返回给前端 get
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")//前端传date类型日期转换 setprivate Date update_time;

public Date getUpdate_time() {
    return update_time;
}

public void setUpdate_time(Date update_time) {
    this.update_time = update_time;
}
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

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

    public Boolean getFalse() {
        return isFalse;
    }

    public void setFalse(Boolean aFalse) {
        isFalse = aFalse;
    }

    public String getBirthday() {
        return birthday;
    }

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

    public User(String userName, Integer age) {
        this.userName = userName;
        this.age = age;
    }
public User(String userName, Integer age, Date update_time) {
    this.userName = userName;
    this.age = age;
    this.update_time = update_time;
}
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", age=" + age +
                ", isFalse=" + isFalse +
                ", birthday='" + birthday + '\'' +
                '}';
    }
}

//fastjson测试类

package com.hlm.com.hlm.test;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hlm.entry.User;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * https://cloud.tencent.com/developer/article/1038066
 */
public class JsonTest {
    private static Logger logger = LoggerFactory.getLogger(JsonTest.class);

   
@Test
    public void test2() {
        List> list = new ArrayList<>();
        List lsits1 = new ArrayList();
        List lsits2 = new ArrayList();
        List lsits3 = new ArrayList();
        lsits1.add(new User("笑话1", 1,new Date()));
        lsits2.add(new User("笑话2", 2,new Date()));
        lsits3.add(new User("笑话3", 3,new Date()));
        list.add(lsits1);
        list.add(lsits2);
        list.add(lsits3);
        //先打印下这个list
        System.out.println(list.toString() + "----------");
        // JSONObject data=(JSONObject)JSON.toJSON(list);//错误用法
        //直接转list
        List> data=JSONObject.parseObject(JSON.toJSONString(list), new TypeReference< List>>() {
        });//转JSon对象的正确用法
        //输出反序列化
        System.out.println(JSONObject.toJSONString(data)+"++++++++++++++++++++++");
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("a", list);
        //打印下json序列化后的字符串
        System.out.println(jsonObj.toJSONString());
        String str = jsonObj.toJSONString();

        List> list2 = jsonObj.getObject("a", new TypeReference>>() {
        });
        //直接在jsonObj对象中取a
        System.out.println(JSONObject.toJSONString(list2));
        //在json序列化成字符串后,在字符串中取反序列化后的map对象转换
        Map>> params = JSONObject.parseObject(str, new TypeReference>>>() {
        });
        //输出序列化后的map对象
        System.out.println(JSONObject.toJSONString(params));
        //在map中取list
        List> list3 = params.get("a");
        //输出序列化后的list对象
        System.out.println(JSONObject.toJSONString(list3));
    }
}

你可能感兴趣的:(json)