fastjson解析对象中的坑

最近敲demo时JSON.parseObject,遇到了一个坑,json字符串解析对象时有两个字段一直赋值不正确,json字符串中字段值=1,但解析出来的对象字段值=0。看了一下正跟一个构造函数的默认赋值相吻合。代码如下:

public static class Funnel {
    int capacity;
    float leakingRate;
    int leftQuota;
    long leakingTs;

    public Funnel(int capacity, float leakingRate) {
        this.capacity = capacity;
        this.leakingRate = leakingRate;
        this.leftQuota = capacity;
        this.leakingTs = 0;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public float getLeakingRate() {
            return leakingRate;
    }

        public void setLeakingRate(float leakingRate) {
            this.leakingRate = leakingRate;
        }

        public int getLeftQuota() {
            return leftQuota;
        }

        public void setLeftQuota(int leftQuota) {
            this.leftQuota = leftQuota;
        }

        public long getLeakingTs() {
            return leakingTs;
        }

        public void setLeakingTs(long leakingTs) {
            this.leakingTs = leakingTs;
        }

    public static void main(String[] args){
        String funnelObj = jedis.get("key");
        Funnel funnel = null;
        if (funnelObj!=null){
            //解析后funnel对象与funnelObject数据不一致
            funnel = JSON.parseObject(funnelObj, Funnel.class);
        }
    }

}

跟了下源码,发现

 

最后添加了空构造函数,即可正常解析

展开阅读全文

没有更多推荐了,返回首页