反序列化json,将子类对象提升到顶层而不是嵌套类

时间:2019-09-27 10:54:01

标签: java json gson deserialization

我正在反序列化来自api的json响应,我有一个有效的代码,但是我唯一关心的是Object类中的元素,如何完全消除Object类并仅返回元素直接来自ListUsers类?

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class ListUsers{

    @SerializedName("meta")
    @Expose
    private Meta meta;
    @SerializedName("objects")
    @Expose
    private List<Objects> objects = null;

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public List<Objects> getObjects() {
        return objects;
    }

    public void setObjects(List<Objects> objects) {
        this.objects = objects;
    }

    private class Meta {
        @SerializedName("limit")
        @Expose
        private int limit;
        @SerializedName("offset")
        @Expose
        private int offset;
        @SerializedName("total_count")
        @Expose
        private int totalCount;

        public int getLimit() {
            return limit;
        }

        public void setLimit(int limit) {
            this.limit = limit;
        }

        public int getOffset() {
            return offset;
        }

        public void setOffset(int offset) {
            this.offset = offset;
        }

        public int getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Meta)) return false;
            Meta meta = (Meta) o;
            return getLimit() == meta.getLimit() &&
                    getOffset() == meta.getOffset() &&
                    getTotalCount() == meta.getTotalCount();
        }

        @Override
        public int hashCode() {
            return java.util.Objects.hash(getLimit(), getOffset(), getTotalCount());
        }

        @Override
        public String toString() {
            return "Meta{" +
                    "limit=" + limit +
                    ", offset=" + offset +
                    ", totalCount=" + totalCount +
                    '}';
        }
    }

    private class Objects {
        @SerializedName("address_one")
        @Expose
        public String addressOne;
        @SerializedName("address_three")
        @Expose
        public String addressThree;
        @SerializedName("address_two")
        @Expose
        public String addressTwo;
        @SerializedName("country_code")
        @Expose
        public String countryCode;
        @SerializedName("cs_domain_id")
        @Expose


        public String getAddressOne() {
            return addressOne;
        }

        public void setAddressOne(String addressOne) {
            this.addressOne = addressOne;
        }

        public String getAddressThree() {
            return addressThree;
        }

        public void setAddressThree(String addressThree) {
            this.addressThree = addressThree;
        }

        public String getAddressTwo() {
            return addressTwo;
        }

        public void setAddressTwo(String addressTwo) {
            this.addressTwo = addressTwo;
        }

        public String getCountryCode() {
            return countryCode;
        }

        public void setCountryCode(String countryCode) {
            this.countryCode = countryCode;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

写一个Adapter (this is a wikipedia link)

  1. 创建一个类层次结构来表示JSON。 成功,这是您的示例来源。
  2. 创建一个包装JSON表示形式类的适配器类。 这将暴露blam0-blah的东西 (在您的情况下,名为Objects的列表) 是一种有趣且富有创造力的方式(也许是getAddressLine1方法之类。)

根据适配器的使用方式, 您可以将List<Objects>解析为Map<Objects>,以方便使用。