fastjson 私有属性的坑

前端之家收集整理的这篇文章主要介绍了fastjson 私有属性的坑前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用fastjson 序列化实体类时 当属性为私有时 会出现错误

public class CancelReason {
    private String cancelReason;

    public CancelReason(String cancelReason) {
        this.cancelReason = cancelReason;
    }

	public String getcancelReason() {
		return cancelReason;
	}

	public void setcancelReason(String cancelReason) {
		this.cancelReason = cancelReason;
	}
    
    

}
JSON.toJSONString(new CancelReason("xxx"))得出的结果是{}


public class CancelReason {
    public String cancelReason;

    public CancelReason(String cancelReason) {
        this.cancelReason = cancelReason;
    }

	public String getcancelReason() {
		return cancelReason;
	}

	public void setcancelReason(String cancelReason) {
		this.cancelReason = cancelReason;
	}
    
    

}
JSON.toJSONString(new CancelReason("xxx"))得出的结果是{"cancelReason":"xxx"}


而用gson 不论上述那种情况都能正确解析 o(︶︿︶)o 唉

猜你在找的Json相关文章