FastJson中@JSONField注解使用

由于Json中的key与bean中的属性不能匹配,在转换过程中出现部分属性为null的情况,此时可以使用@JSONField进行解释。

参考:FastJson及Gson定义字段别名

@JSONField的作用对象:

  1. Field
  2. Setter 和 Getter方法

注:FastJson操作时,是根据Getter和Setter的方法进行的,并不是依据Field进行

Address.java

public class Address {
    @JSONField(name = "address_id")
    private long id;
    @JSONField(name = "user_id")
    private long userId;
    private String name;
    private String mobile;
    private Region country;
    private Region state;
    private Region city;
    private Region district;
    @JSONField(name = "address_detail")
    private String addressDetail;
    @JSONField(name = "post_code")
    private String postcode;
    @JSONField(serialize = false)
    private boolean isDefault;

    public Address() {
    }

    public long getId() {
        return id;
    }

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

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

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

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @JSONField(serialize = false)
    public Region getCountryRegion() {
        return country;
    }

    @JSONField(name = "country")
    public String getCountry() {
        return country.getName();
    }

    @JSONField(serialize = false)
    public void setCountryRegion(Region country) {
        this.country = country;
    }

    @JSONField(name = "country")
    public void setCountry(String country) {
        Region region = new Region();
        region.setName(country);
        this.country = region;
    }

    @JSONField(serialize = false)
    public Region getStateRegion() {
        return state;
    }

    @JSONField(name = "state")
    public String getState() {
        return state.getName();
    }

    @JSONField(serialize = false)
    public void setStateRegion(Region state) {
        this.state = state;
    }

    @JSONField(name = "state")
    public void setState(String state) {
        Region region = new Region();
        region.setName(state);
        this.state = region;
    }

    @JSONField(serialize = false)
    public Region getCityRegion() {
        return city;
    }

    @JSONField(name = "city")
    public String getCity() {
        return city.getName();
    }

    @JSONField(serialize = false)
    public void setCityRegion(Region city) {
        this.city = city;
    }

    @JSONField(name = "city")
    public void setCity(String city) {
        Region region = new Region();
        region.setName(city);
        this.city = region;
    }

    @JSONField(serialize = false)
    public Region getDistrictRegion() {
        return district;
    }

    @JSONField(name = "district")
    public String getDistrict() {
        return district.getName();
    }

    @JSONField(serialize = false)
    public void setDistrictRegion(Region district) {
        this.district = district;
    }

    @JSONField(name = "district")
    public void setDistrict(String district) {
        Region region = new Region();
        region.setName(district);
        this.district = region;
    }

    public String getAddressDetail() {
        return addressDetail;
    }

    public void setAddressDetail(String addressDetail) {
        this.addressDetail = addressDetail;
    }

    public String getPostcode() {
        return postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    public boolean isDefault() {
        return isDefault;
    }

    public void setDefault(boolean aDefault) {
        isDefault = aDefault;
    }
}

Region.java

public class Region {
    private int id;
    private String code;
    private String name;
    private int parentId;

    public Region() {

    }

    public int getId() {
        return id;
    }

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

    public String getCode() {
        return code;
    }

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

    public String getName() {
        return name;
    }

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

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/fastjson-jsonfield-annotation/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
FastJson中@JSONField注解使用
由于Json中的key与bean中的属性不能匹配,在转换过程中出现部分属性为null的情况,此时可以使用@JSONField进行解释。 参考:FastJson及Gson定义字段别名 @JSO……
<<上一篇
下一篇>>
文章目录
关闭
目 录