fastjson 解析字符串(自定义类)

com.alibaba.fastjson


Man类


package test;

import java.util.List;

public class Man {
    //属性和json的键对上
    private String name;// 姓名 
    private String sex;// 性别
    private Integer age;// 年龄
    private String goodAt;// 特长
    private String love;
    private List loveList;

    public Man() {
        super();
    }

    public Man(String name, String sex, Integer age, String goodAt, String love, List loveList) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.goodAt = goodAt;
        this.love = love;
        this.loveList = loveList;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getGoodAt() {
        return goodAt;
    }

    public void setGoodAt(String goodAt) {
        this.goodAt = goodAt;
    }

    public String getLove() {
        return love;
    }

    public void setLove(String love) {
        this.love = love;
    }

    public List getLoveList() {
        return loveList;
    }

    public void setLoveList(List loveList) {
        this.loveList = loveList;
    }

}

Love类


package test;

public class Love {
    private String hobby;
    private String color;

    public Love() {
        super();
    }

    public Love(String hobby, String color) {
        super();
        this.hobby = hobby;
        this.color = color;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

}

测试用例


package test;

import java.util.List;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class FastJsonDemo {
    
    @Test
    public void testFastJsonUseClass() {
        // 依然使用同一个json字符串
        String jsonStr = "{\r\n" + "\"name\":\"jarWorker\",\r\n" + "\"sex\":\"男\",\r\n" + "\"age\":26,\r\n"
                + "\"love\":[{\"hobby\":\"足球\",\"color\":\"White\"},{\"hobby\":\"篮球\",\"color\":\"Brown\"},{\"hobby\":\"\",\"color\":\"Yellow\"}],\r\n"
                + "\"goodAt\":\"Java\"\r\n" + "}";
        Man man = JSON.parseObject(jsonStr, Man.class);//Man对象
        String name=man.getName();
        String sex=man.getSex();
        Integer age=man.getAge();
        String goodAt=man.getGoodAt();
        String love=man.getLove();
        List loveList=man.getLoveList();
        System.out.println("name====" + name);
        System.out.println("sex====" + sex);
        System.out.println("age====" + age);
        System.out.println("goodAt====" + goodAt);
        System.out.println("love====" + love);
        System.out.println("loveList====" + loveList);//null
        
        List loveArray=JSON.parseArray(love, Love.class);//Love集合对象
        for (Love loveEach : loveArray) {          //增强for循环
            String hobby = loveEach.getHobby();
            String color = loveEach.getColor();
            System.out.println("hobby>>>" + hobby);
            System.out.println("color>>>" + color);
            System.out.println("------------------------------------");
        }
        System.out.println("数组中的Love对象个数:" + loveArray.size());
    }
}

Console:输出


name====jarWorker
sex====男
age====26
goodAt====Java
love====[{"color":"White","hobby":"足球"},{"color":"Brown","hobby":"篮球"},{"color":"Yellow","hobby":""}]
loveList====null
hobby>>>足球
color>>>White
===========================
hobby>>>篮球
color>>>Brown
===========================
hobby>>>
color>>>Yellow
===========================
数组中的Love对象个数:3

你可能感兴趣的:(fastjson 解析字符串(自定义类))