Gson报declares multiple JSON fields named XXX解决方法

    因为要把对象转化为json字符串,然后存到redis 中,所以用到了Gson。但是今天在测试的工程中,突然报出一下异常:

class com.lenovoedu.model.adapter.SysAuthorityTreeAdapter declares multiple JSON fields named childList

于是,根据异常信息认真检查异常信息中的类,发现如下:

SysAuthorityTreeAdapter的源码如下:

Gson报declares multiple JSON fields named XXX解决方法_第1张图片

SysAuthority的源码如下:
package com.lenovoedu.model.sys.model;

import com.lenovoedu.base.mybatise.Model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

public class SysAuthority implements Serializable, Model {

    public enum Column{
        id, authorityName, parentId, authorityFlag, authorityType, url, isAble
    }

    /**
     * 主键
     */
    private Long id;
    /**
     * 权限名称
     */
    private String authorityName;
    /**
     * 父亲ID
     */
    private Long parentId;
    /**
     * 权限标识
     */
    private String authorityFlag;
    /**
     * 权限类型1模块2目录3菜单4按钮5其他
     */
    private String authorityType;
    /**
     * 访问地址
     */
    private String url;

    /**
     * 权限状态1正常2删除
     */
    private String isAble;

    private ArrayList childList;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public Long getParentId() {
        return parentId;
    }

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

    public String getAuthorityName() {
        return authorityName;
    }

    public void setAuthorityName(String authorityName) {
        this.authorityName = authorityName;
    }

    public String getAuthorityFlag() {
        return authorityFlag;
    }

    public void setAuthorityFlag(String authorityFlag) {
        this.authorityFlag = authorityFlag;
    }

    public String getAuthorityType() {
        return authorityType;
    }

    public void setAuthorityType(String authorityType) {
        this.authorityType = authorityType;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIsAble() {
        return isAble;
    }

    public void setIsAble(String isAble) {
        this.isAble = isAble;
    }

    public ArrayList getChildList() {
        return childList;
    }

    public void setChildList(ArrayList childList) {
        this.childList = childList;
    }
}

也有属性childList。所以当我用Gson转SysAuthorityTreeAdapter对象的时候,就会报错。说明父类和子类中的属性重复。

解决办法:

1.修改其中一个类中相同属性的属性名即可。

2.我的解决办法,我发现我往redis存父类和子类是一样的效果,所以我什么也没有改,果断存了子类SysAuthority

Gson报declares multiple JSON fields named XXX解决方法_第2张图片

你可能感兴趣的:(Gson报declares multiple JSON fields named XXX解决方法)