.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to

时间:2022-01-07 15:52:09

这种是类型转换中断。

DataEntity 也必须要加<T>, 不光List<T>

否则类型转换会在 DataEntity 中断,导致类型转换异常。

private DataEntity<T> data;

@Override
public String toString() {
    return "BaseResults{" +
            "data=" + data +
            '}';
}

public DataEntity<T> getData() {
    return data;
}

public List<T> getList() {
    return data.rows;
}

public class DataEntity<T> {

    @Override
    public String toString() {
        return "DataEntity{" +
                "total=" + total +
                ", rows=" + rows +
                '}';
    }

    private int total;

    private List<T> rows;

    public void setTotal(int total) {
        this.total = total;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public int getTotal() {
        return total;
    }

    public List<T> getRows() {
        return rows;
    }

}