FastJson之json字符串与javabean的互相转换

1.基本使用说明

FastJson常用到一下三个类:

(1)JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。

(2)JSONObject:fastJson提供的json对象。

(3)JSONArray:fastJson提供json数组对象。

 

2.案例

public class Student {

    private String id;

    @JSONField(name="myName")  //自定义字段名字
    private String name;

    @JSONField(serialize=false)  //是否加入到序列化和反序列化中,true为加入,false为不加入,默认为true
    private Integer age;

    @JSONField(format="yyyy-MM-dd hh:mm:ss")  //格式化时间字段
    private Date birthday;

    private List teacherList;

    private Teacher teacher;

    private Map data;

    private List> dataList;

    private Integer sex;

    private Float weight;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

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

    public Date getBirthday() {
        return birthday;
    }

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

    public List getTeacherList() {
        return teacherList;
    }

    public void setTeacherList(List teacherList) {
        this.teacherList = teacherList;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public Map getData() {
        return data;
    }

    public void setData(Map map) {
        this.data = map;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Float getWeight() {
        return weight;
    }

    public void setWeight(Float weight) {
        this.weight = weight;
    }

    public List> getDataList() {
        return dataList;
    }

    public void setDataList(List> dataList) {
        this.dataList = dataList;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", teacherList=" + teacherList +
                ", teacher=" + teacher +
                ", data=" + data +
                ", dataList=" + dataList +
                ", sex=" + sex +
                ", weight=" + weight +
                '}';
    }

}

 

public class Teacher {

    private String id;

    private String name;

    private Integer age;

    public Teacher() {

    }

    public Teacher(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Teacher{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}

 

public class Main {

    public static void main(String[] args) {

        Student stu = new Student();
        initData(stu);
        //bean转为json字符串
        beanToJson(stu);

        //json字符串转为bean
        String jsonStr = "{\"age\":25,\"birthday\":1543657701694,\"data\":{\"1\":\"小罗\",\"2\":\"小嘉\",\"3\":\"小敏\"},\"dataList\":[{\"1\":\"小罗\",\"2\":\"小嘉\",\"3\":\"小敏\"},{\"1\":\"小周\",\"2\":\"小蕾\"}],\"id\":\"007\",\"name\":\"小西\",\"sex\":0,\"teacher\":{\"age\":22,\"id\":\"1\",\"name\":\"佳佳\"},\"teacherList\":[{\"age\":22,\"id\":\"1\",\"name\":\"小明\"},{\"age\":22,\"id\":\"2\",\"name\":\"小红\"},{\"age\":22,\"id\":\"3\",\"name\":\"小志\"}],\"weight\":62.5}";
        jsonToBean(jsonStr);

        //方式2:json字符串转为bean
        jsonToBean2(jsonStr);

    }

    public static void beanToJson(Student stu) {
        //解析为json字符串的时候关闭循环引用
        String jsonStr = JSON.toJSONString(stu, SerializerFeature.DisableCircularReferenceDetect);
        System.out.println(jsonStr);
    }

    public static void jsonToBean(String jsonStr) {
        /** 两种方式反序列化 **/
//        Student stu = JSON.parseObject(jsonStr, Student.class);
        Student stu = JSON.parseObject(jsonStr, new TypeReference(){});
        System.out.println(stu);
    }

    public static void jsonToBean2(String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        Student stu = JSON.parseObject(jsonObject.toJSONString(), new TypeReference(){});
        System.out.println(stu);
    }

    public static void initData(Student stu) {
        stu.setId("007");
        stu.setName("小西");
        stu.setAge(25);
        stu.setBirthday(new Date());
        stu.setSex(0);
        stu.setWeight(62.5f);
        //list集合,set集合一样
        ArrayList list = new ArrayList<>();
        list.add(new Teacher("1","小明",22));
        list.add(new Teacher("2","小红",22));
        list.add(new Teacher("3","小志",22));
        stu.setTeacherList(list);
        //在javaBean中添加javaBean
        Teacher teacher = new Teacher("1","佳佳",22);
        stu.setTeacher(teacher);
        //添加map集合
        Map map = new HashMap<>();
        map.put("1", "小罗");
        map.put("2", "小嘉");
        map.put("3", "小敏");
        stu.setData(map);
        //添加map泛型的list
        List> dataList = new ArrayList<>();
        Map map2 = new HashMap<>();
        map2.put("1", "小周");
        map2.put("2", "小蕾");
        dataList.add(map);
        dataList.add(map2);
        stu.setDataList(dataList);
    }

}

 

你可能感兴趣的:(java)