FastJson jsonString 转 Object

FastJson jsonString 转 Object

Result 对象:

public class Result {
    private Integer code;
    private String msg;
    private ArrayList<Data> data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public ArrayList<Data> getData() {
        return data;
    }

    public void setData(ArrayList<Data> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}

Data 对象:

public class Data {
    private String clsName;
    private Double score;
    private List<Integer> bbox;
    private Integer cls;

    public String getClsName() {
        return clsName;
    }

    public void setClsName(String clsName) {
        this.clsName = clsName;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public List<Integer> getBbox() {
        return bbox;
    }

    public void setBbox(List<Integer> bbox) {
        this.bbox = bbox;
    }

    public Integer getCls() {
        return cls;
    }

    public void setCls(Integer cls) {
        this.cls = cls;
    }

    @Override
    public String toString() {
        return "Data{" +
                "clsName='" + clsName + '\'' +
                ", score=" + score +
                ", bbox=" + bbox +
                ", cls=" + cls +
                '}';
    }
}

参考代码:

@Test
void jsonFor() {
    // TODO 字符串处理,将score>0.7数据返回即可
    String str = "{code:200, msg:'test', data:[{\"cls_name\":\"“元”\",\"score\":0.18183870613574982,\"bbox\":[207,50,243,91],\"cls\":4},{\"cls_name\":\"“元”\",\"score\":0.18710173666477203,\"bbox\":[226,50,263,90],\"cls\":4}]}";
    Result result = JSON.parseObject(str, Result.class);
    ArrayList<Data> newData = new ArrayList<>();
    if (result.getCode() == 200) {
        for (Data data : result.getData()) {
            if (data.getScore() >= 0.182) {
                newData.add(data);
            }
        }
        result.setData(newData);
    }
    System.out.println(result);
    System.out.println(JSON.toJSONString(result));
}
posted @ 2021-01-21 23:48  GetcharZp  阅读(215)  评论(0编辑  收藏  举报