Sri Sri - 1 month ago 5
JSON Question

Issue with deserializing nested json

I am working on deserializing the below JSON:

{"self":"http://members.cs.com/rest/api/user?username=abc@cs.com",
"key":"abc@cs.com",
"name":"abc@cs.com",
"emailAddress":"abc@cs.com",
"displayName":"ABC",
"active":true,
"members":{"size":1,"items":[{"name":"member-users","self":"http://members.cs.com/rest/api/user?username=abc@cs.com"}]},
"expand":"members"}


I have created the following classes:

@JsonIgnoreProperties(ignoreUnknown = true)
public class UserList {

private String name;
private String emailAddress;
private String displayName;
private boolean active;
List<MemberName> members = new ArrayList<>();

@JsonCreator
public UserList(@JsonProperty("name") String name, @JsonProperty("emailAddress") String emailAddress, @JsonProperty("displayName") String displayName, @JsonProperty("active") boolean active, @JsonProperty("members") List<MemberName> members) {
this.name = name;
this.emailAddress = emailAddress;
this.displayName = displayName;
this.active = active;
this.members.addAll(groups);
}

//getters
}


@JsonIgnoreProperties(ignoreUnknown = true)
public class MemberName {
private String name;

@JsonCreator
public MemberName(@JsonProperty("name") String name) {
this.name = name;
}

public String getName() {
return name;
}

}


When I don't give the members as a property the deserialization works fine and I can see the values for the name, displayName, active, emailAddress. The problem happens with the MemberName.

Could someone help with this?

Answer

This worked for me:

Class Items as below:

public class Items {
    private String name;
    private String self;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSelf() {
        return self;
    }
    public void setSelf(String self) {
        this.self = self;
    }
}

Members class as below:

public class Members {
    private int size;
    private List<Items> items;
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public List<Items> getItems() {
        return items;
    }
    public void setItems(List<Items> items) {
        this.items = items;
    }
}

Data class as below:

public class Data {
    private String self;
    private String key;
    private String name;
    private String emailAddress;
    private String displayName;
    private boolean active;
    private Members members;
    private String expand;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSelf() {
        return self;
    }
    public void setSelf(String self) {
        this.self = self;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    public String getDisplayName() {
        return displayName;
    }
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
    public boolean isActive() {
        return active;
    }
    public void setActive(boolean active) {
        this.active = active;
    }
    public Members getMembers() {
        return members;
    }
    public void setMembers(Members members) {
        this.members = members;
    }
    public String getExpand() {
        return expand;
    }
    public void setExpand(String expand) {
        this.expand = expand;
    }
}

The deserialization as below:

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

        String jsonString = "{\"self\": \"http://members.cs.com/rest/api/user?username=abc@cs.com\",\"key\": \"abc@cs.com\",\"name\": \"abc@cs.com\","
                + "\"emailAddress\": \"abc@cs.com\",\"displayName\": \"ABC\",\"active\": true,\"members\": {\"size\": 1,\"items\": [{"
                + "\"name\": \"member-users\",\"self\": \"http://members.cs.com/rest/api/user?username=abc@cs.com\" }]},\"expand\": \"members\"}";
        ObjectMapper mapper = new ObjectMapper();
        Data obj = mapper.readValue(jsonString,Data.class);

        System.out.println(obj.getSelf());
        System.out.println(obj.getKey());
        System.out.println(obj.getName());
        System.out.println(obj.getEmailAddress());
        System.out.println(obj.getDisplayName());
        System.out.println(obj.isActive());
        System.out.println(obj.getMembers().getSize());
        System.out.println(obj.getMembers().getItems().get(0).getName());
        System.out.println(obj.getMembers().getItems().get(0).getSelf());
        System.out.println(obj.getExpand());
}
Comments