Проблема разбора json с помощью GsonConverterFactory Retrofit - PullRequest
0 голосов
/ 01 апреля 2020

Я хочу преобразовать следующий результат JSON в Pojos в Retrofit:

{
    "status": "success",
    "body": {
        "token": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQi",
        "profile": [
            {
                "id": 18,
                "first_name": "",
                "last_name": "",
                "mobile": "0000000",
                "email": null,
                "code_melli": null,
                "image": "http://example.com/image.jpg",
                "address": null,
                "credit": 0,
                "status": "active",
                "gender": "male",
                "role": "customer",
                "created_at": "2020-02-02"
            }
        ]
    }
}

Мои классы моделей следующие:

public class Customer {

    @SerializedName("status")
    private String status;
    @SerializedName("body")
    private Body body;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }

}

и Body,

public class Body {

    @SerializedName("token")
    private String token;
    @SerializedName("profile")
    private List<Profile> profile = null;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public List<Profile> getProfile() {
        return profile;
    }

    public void setProfile(List<Profile> profile) {
        this.profile = profile;
    }

}

и Profile is,

 public class Profile {



         @SerializedName("id")
            @Nullable
            private Integer id;
            @SerializedName("first_name")
            @Nullable
            private String firstName;
            @SerializedName("last_name")
        @Nullable
        private String lastName;
        @SerializedName("mobile")
        @Nullable
        private String mobile;
        @SerializedName("email")
        @Nullable
        private Object email;
        @SerializedName("code_melli")
        @Nullable
        private Object codeMelli;
        @SerializedName("image")
        @Nullable
        private String image;
        @SerializedName("address")
        @Nullable
        private Object address;
        @SerializedName("credit")
        @Nullable
        private Integer credit;
        @SerializedName("status")
        @Nullable
        private String status;
        @SerializedName("gender")
        @Nullable
        private String gender;
        @SerializedName("role")
        @Nullable
        private String role;
        @SerializedName("created_at")
        @Nullable
        private String createdAt;

        public Integer getId() {
            return id;
        }

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

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getMobile() {
            return mobile;
        }

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

        public Object getEmail() {
            return email;
        }

        public void setEmail(Object email) {
            this.email = email;
        }

        public Object getCodeMelli() {
            return codeMelli;
        }

        public void setCodeMelli(Object codeMelli) {
            this.codeMelli = codeMelli;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public Object getAddress() {
            return address;
        }

        public void setAddress(Object address) {
            this.address = address;
        }

        public Integer getCredit() {
            return credit;
        }

        public void setCredit(Integer credit) {
            this.credit = credit;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public String getCreatedAt() {
            return createdAt;
        }

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

Но проблема в том, что в ответе я могу получить значение status и проверить его на успешность, однако любой метод в профиле дает нулевой указатель Исключение составляет код для доступа к полям профиля:

Customer customer = response.body();
 customer.getBody().getProfile().get(0).getFirstName();

1 Ответ

0 голосов
/ 04 апреля 2020

Я использую следующий gson для модернизации, и все работает

 Gson gson = new GsonBuilder()
                    .serializeNulls()
                    .setLenient()
                    .create();
...