0

I cannot properly convert a JSON Object into POJO. I kind of understand where is the problem, but can't figure out, how to deal with it.

Here's all specific data to understand the issue:

JSONObject which I try to deserialize (understanding the values names isn't key to understand the problem):

[{"name":"Rafał","description":"Przykładowy opis profilu","location":"Lublin","interests":[{"0":"Gry komputerowe","1":"Muzyka","2":"Siłownia"}],"age":24,"rowid":2,"username":"lenivius"}]

My POJO class:

public class Users {
    private int rowid = 0, age;
    private String name, username, e_mail, password, description, location;
    private List<String> interests;

    public Users() {

    }

    public Users(int rowid, int age, String name, String username, String e_mail, String password, String description, String location, List<String> interests) {
        this.setRowid(rowid);
        this.setAge(age);
        this.setName(name);
        this.setUsername(username);
        this.setE_mail(e_mail);
        this.setPassword(password);
        this.setDescription(description);
        this.setLocation(location);
        this.setInterests(interests);
    }

    public int getRowid() {
        return rowid;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getUsername() {
        return username;
    }

    public String getE_mail() {
        return e_mail;
    }

    public String getPassword() {
        return password;
    }

    public String getDescription() {
        return description;
    }

    public String getLocation() {
        return location;
    }

    public void setRowid(int rowid) {
        this.rowid = rowid;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    public void setE_mail(String e_mail) {
        this.e_mail = e_mail;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public List<String> getInterests() {
        return interests;
    }

    public void setInterests(List<String> interests) {
        this.interests = interests;
    }
}

And lastly code line which causes the exception to happen:

resultUsers = objectMapper.readValue(responseString, Users[].class);

I can also post full exception message:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String out of START_OBJECT token at [Source: (String)"[{"name":"Rafał","description":"Przykładowy opis profilu","location":"Lublin","interests":[{"0":"Gry komputerowe","1":"Muzyka","2":"Siłownia"}],"age":24,"rowid":2,"username":"lenivius"}]"; line: 1, column: 92] (through reference chain: java.lang.Object[][0]->com.example.loveterests.Users["interests"]->java.util.ArrayList[0])

If I understand the problem correctly, then Jackson needs a list of String objects to properly convert JSON into POJO, but inside "interests" key there is a JSON Array, and that's probably where all the is the problem.

2
  • Obviously interests is not a list of string in json but list of objects Commented Dec 31, 2020 at 1:47
  • .. In fact list contains a single object which is dictionary / map Commented Dec 31, 2020 at 1:51

1 Answer 1

1

Problem is with interests property. In POJO it is represented by List<String> and in JSON Payload by JSON Array[JSON Object] - array with objects, not strings. You can use Map<String, Object> type to handle this:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
class Users {
    private int rowid = 0, age;
    private String name, username, e_mail, password, description, location;
    private List<Map<String, Object>> interests;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for Your answer. So let me ask You if I'm correct: the list will contain a map of Strings containing key values (in my example "0", "1", "2" and Objects containing names of interests?
Nevermind, figured it out. Your answer did solve my problem, thanks a lot kind sir!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.