无法将json转换为java pojo类?

时间:2019-02-01 04:20:06

标签: java android json retrofit

我想将此JSON转换为列表并插入RecyclerView中,但我不能,因为它给了我3个类。 因此,如何将此类转换为父类?谁能帮助我,以便我列出要放入RecyclerView的响应列表。

[
    {
        "id": 114,
        "name_ar": "محمصه زين",
        "latitude": null,
        "longitude": null,
        "name_en": "Mohamsa Zein",
        "description_ar": "",
        "description_en": "",
        "phone": null,
        "mobile": "01110881085",
        "address_ar": "مدينتى، Egypt",
        "address_en": "مدينتى، Egypt",
        "photo": "5b7958dc1c3661534679260.jpg",
        "album": null,
        "created_at": {
            "date": "2018-08-19 11:47:40.000000",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "updated_at": {
            "date": "2018-10-23 11:00:22.000000",
            "timezone_type": 3,
            "timezone": "UTC"
        }
    },
    {
        "id": 184,
        "name_ar": "محل عصائر فيرتاس",
        "latitude": null,
        "longitude": null,
        "name_en": "Varieties For Juice",
        "description_ar": "",
        "description_en": "",
        "phone": null,
        "mobile": "01024229935",
        "address_ar": "مدينتى، Egypt",
        "address_en": "مدينتى، Egypt",
        "photo": null,
        "album": null,
        "created_at": {
            "date": "2018-10-28 11:40:54.000000",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "updated_at": {
            "date": "2019-01-02 08:36:04.000000",
            "timezone_type": 3,
            "timezone": "UTC"
        }
    }
]

3 个答案:

答案 0 :(得分:3)

我对此的处理方法是:

使用GSON库。

我创建2个模型类

  1. 包含每个数组项信息的主类
  2. 自定义日期类,其中将包含您的日期信息 @RequestMapping(value = "/search") @Cacheable(value = "halfHourCache", keyGenerator = "haodfKeyGenerator") public ResponseEntity<String> search(BizData bizData, Page page, String sourceType) { return new ResponseEntity<>(new Gson().toJson(SearchSourceRunner.search(bizData, page, sourceType, DiseaseKeyWorldFilter.class)), HttpStatus.OK); } created_at属性

    updated_at

    class MyDate{ public String date; public int timezone_type; public String timezone }

然后将json解析为Info类的数组列表

class Info{
    public String id;
    public String name_ar;
    ..//other properties//..
    public MyDate created_at;
    public MyDate updated_at;
}

答案 1 :(得分:1)

如果我正确理解您的问题,我相信这是您的意思。请使用

public  class TestRequest {

    private UpdatedAtEntity updatedAt;
    private CreatedAtEntity createdAt;
    private String photo;
    private String addressEn;
    private String addressAr;
    private String mobile;
    private String descriptionEn;
    private String descriptionAr;
    private String nameEn;
    private String nameAr;
    private int id;

    public UpdatedAtEntity getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(UpdatedAtEntity updatedAt) {
        this.updatedAt = updatedAt;
    }

    public CreatedAtEntity getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(CreatedAtEntity createdAt) {
        this.createdAt = createdAt;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getAddressEn() {
        return addressEn;
    }

    public void setAddressEn(String addressEn) {
        this.addressEn = addressEn;
    }

    public String getAddressAr() {
        return addressAr;
    }

    public void setAddressAr(String addressAr) {
        this.addressAr = addressAr;
    }

    public String getMobile() {
        return mobile;
    }

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

    public String getDescriptionEn() {
        return descriptionEn;
    }

    public void setDescriptionEn(String descriptionEn) {
        this.descriptionEn = descriptionEn;
    }

    public String getDescriptionAr() {
        return descriptionAr;
    }

    public void setDescriptionAr(String descriptionAr) {
        this.descriptionAr = descriptionAr;
    }

    public String getNameEn() {
        return nameEn;
    }

    public void setNameEn(String nameEn) {
        this.nameEn = nameEn;
    }

    public String getNameAr() {
        return nameAr;
    }

    public void setNameAr(String nameAr) {
        this.nameAr = nameAr;
    }

    public int getId() {
        return id;
    }

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

    public static class UpdatedAtEntity {
        private String timezone;
        private int timezoneType;
        private String date;

        public String getTimezone() {
            return timezone;
        }

        public void setTimezone(String timezone) {
            this.timezone = timezone;
        }

        public int getTimezoneType() {
            return timezoneType;
        }

        public void setTimezoneType(int timezoneType) {
            this.timezoneType = timezoneType;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }

    public static class CreatedAtEntity {
        private String timezone;
        private int timezoneType;
        private String date;

        public String getTimezone() {
            return timezone;
        }

        public void setTimezone(String timezone) {
            this.timezone = timezone;
        }

        public int getTimezoneType() {
            return timezoneType;
        }

        public void setTimezoneType(int timezoneType) {
            this.timezoneType = timezoneType;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }

    @Override
    public String toString() {
        return "TestRequest{" +
                "updatedAt=" + updatedAt +
                ", createdAt=" + createdAt +
                ", photo='" + photo + '\'' +
                ", addressEn='" + addressEn + '\'' +
                ", addressAr='" + addressAr + '\'' +
                ", mobile='" + mobile + '\'' +
                ", descriptionEn='" + descriptionEn + '\'' +
                ", descriptionAr='" + descriptionAr + '\'' +
                ", nameEn='" + nameEn + '\'' +
                ", nameAr='" + nameAr + '\'' +
                ", id=" + id +
                '}';
    }
}

答案 2 :(得分:0)

像这样创建pojo类。

public abstract class TempClass {

    @Expose
    @SerializedName("updated_at")
    private UpdatedAt updatedAt;
    @Expose
    @SerializedName("created_at")
    private CreatedAt createdAt;
    @Expose
    @SerializedName("photo")
    private String photo;
    @Expose
    @SerializedName("address_en")
    private String addressEn;
    @Expose
    @SerializedName("address_ar")
    private String addressAr;
    @Expose
    @SerializedName("mobile")
    private String mobile;
    @Expose
    @SerializedName("description_en")
    private String descriptionEn;
    @Expose
    @SerializedName("description_ar")
    private String descriptionAr;
    @Expose
    @SerializedName("name_en")
    private String nameEn;
    @Expose
    @SerializedName("name_ar")
    private String nameAr;
    @Expose
    @SerializedName("id")
    private int id;

    public UpdatedAt getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(UpdatedAt updatedAt) {
        this.updatedAt = updatedAt;
    }

    public CreatedAt getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(CreatedAt createdAt) {
        this.createdAt = createdAt;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getAddressEn() {
        return addressEn;
    }

    public void setAddressEn(String addressEn) {
        this.addressEn = addressEn;
    }

    public String getAddressAr() {
        return addressAr;
    }

    public void setAddressAr(String addressAr) {
        this.addressAr = addressAr;
    }

    public String getMobile() {
        return mobile;
    }

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

    public String getDescriptionEn() {
        return descriptionEn;
    }

    public void setDescriptionEn(String descriptionEn) {
        this.descriptionEn = descriptionEn;
    }

    public String getDescriptionAr() {
        return descriptionAr;
    }

    public void setDescriptionAr(String descriptionAr) {
        this.descriptionAr = descriptionAr;
    }

    public String getNameEn() {
        return nameEn;
    }

    public void setNameEn(String nameEn) {
        this.nameEn = nameEn;
    }

    public String getNameAr() {
        return nameAr;
    }

    public void setNameAr(String nameAr) {
        this.nameAr = nameAr;
    }

    public int getId() {
        return id;
    }

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

    public static class UpdatedAt {
        @Expose
        @SerializedName("timezone")
        private String timezone;
        @Expose
        @SerializedName("timezone_type")
        private int timezoneType;
        @Expose
        @SerializedName("date")
        private String date;

        public String getTimezone() {
            return timezone;
        }

        public void setTimezone(String timezone) {
            this.timezone = timezone;
        }

        public int getTimezoneType() {
            return timezoneType;
        }

        public void setTimezoneType(int timezoneType) {
            this.timezoneType = timezoneType;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }

    public static class CreatedAt {
        @Expose
        @SerializedName("timezone")
        private String timezone;
        @Expose
        @SerializedName("timezone_type")
        private int timezoneType;
        @Expose
        @SerializedName("date")
        private String date;

        public String getTimezone() {
            return timezone;
        }

        public void setTimezone(String timezone) {
            this.timezone = timezone;
        }

        public int getTimezoneType() {
            return timezoneType;
        }

        public void setTimezoneType(int timezoneType) {
            this.timezoneType = timezoneType;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }
}

现在像这样访问内部类。请在下面检查。

ArrayList<TempClass> item = new ArrayList<>();

item.get(holder.getAdapterPosition()).getUpdatedAt().getDate();//here access the UpdatedAt class object method. 

item.get(holder.getAdapterPosition()).getCreatedAt().getDate();// here access the CreatedAt class object method.