0

I'm trying to grab the contents of an external web service which returns an array of users in JSON format, then convert the JSON response into an array of EclipseUser objects. The response from the web service wraps the array of users in an array labelled 'users'. I've done some research but can't seem to find an example that shows me how to grab the items in the user array, and then convert them to Objects.

{
users: [
{
id: 2,
name: "David Cook",
username: "david.cook",
password: "12345",
email: "[email protected]"
},
{
id: 5,
name: "Kieran Barnby",
username: "kieran.barnby",
password: "12345",
email: "[email protected]
}
]
}

I'm wondering how I would go about retrieving the JSON and then converting it to an array of objects. Here's the object class:

public class EclipseUser {

    public int id;
    public String name;
    public String username;
    public String password;
    public String email;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
1
  • Looks like you are asking how to automatically bind JSON to Java objects and vice-versa, have you tried Jackson library? From the documentation I see that Spring boot already has MappingJackson2MessageConverter Commented Nov 23, 2015 at 15:59

2 Answers 2

3

You have to use Jackson library to convert JSON to Object or vice-versa. Other than that you have to create aditional java Object to get list of users.

  Class Users{
      @JsonProperty(“users”)
      private List<EclipseUser> users;
      //getters and setters
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You should create different class which will be contain an array od EclipseUser objects, after that current JSON will be convert correct. Currently you have array of user, but it don't map to EclipseUser, because EclipseUser it's only one user.

Comments

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.