1

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?

Sri
  • 573
  • 2
  • 6
  • 20
  • Is parsing from XML an option for this web API? I've run into similar problems trying to parse from JSON, and I ended up needing to switch to XML because it can be more explicit than JSON especially when it comes to complex nested types like this. A lot of times web API's can provide both JSON or XML, you just have to specify the type of response that you want. – TJ Rockefeller Oct 27 '16 at 16:15
  • Parsing from XML is not an option as we plan to have a common approach for all the service calls in our application. The work with JSON is already implemented in many other calls and I don't want to change it to XML for this one. – Sri Oct 27 '16 at 16:33
  • I think this question may be related. http://stackoverflow.com/questions/9829403/deserialize-json-to-arraylistpojo-using-jackson – TJ Rockefeller Oct 27 '16 at 16:45

1 Answers1

1

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());
}
HARDI
  • 394
  • 5
  • 12