0

Here is the data being passed into me. I have no control over the structure of this data:

{
   "getParentCommunications":{
      "ccList":[
         {
            "personId":12,
            "parentFirstNm":"johnny"
         },
         {
            "personId":14,
            "parentFirstNm":"Sue"
         },
         {
            "personId":19,
            "parentFirstNm":"Ashley"
         }
     ]
   }
}

I want need to be able to get the ccList data and view the information in there. I am trying to put he data into a Java class so I can manipulate it as needed.

using Jackson, I have this code

ClientResponse response = webResource.post(ClientResponse.class, input);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
GetParentCommunications gpc = mapper.readValue(response.getEntity(String.class), GetParentCommunications.class);
                
List<CcList> cclist = gpc.getCcList();
System.out.println( "size " + cclist.size());  //(produces a java.lang.NullPointerException)

Finally, my two classes that I thought would give me the data I wanted. GetParentCommunications

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder({
"ccList"
})
public class GetParentCommunications implements Serializable
{

@JsonProperty("ccList")
private List<CcList> ccList = null;

@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
private final static long serialVersionUID = 8422191816885827338L;

@JsonProperty("ccList")
public List<CcList> getCcList() {
return ccList;
}

@JsonProperty("ccList")
public void setCcList(List<CcList> ccList) {
this.ccList = ccList;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

and CcList

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder({
"personId",
"parentFirstNm"
})
public class CcList {

@JsonProperty("personId")
private Integer personId;
@JsonProperty("parentFirstNm")
private String parentFirstNm;

@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("personId")
public Integer getPersonId() {
return personId;
}

@JsonProperty("personId")
public void setPersonId(Integer personId) {
this.personId = personId;
}

@JsonProperty("parentFirstNm")
public String getParentFirstNm() {
return parentFirstNm;
}

@JsonProperty("parentFirstNm")
public void setParentFirstNm(String parentFirstNm) {
this.parentFirstNm = parentFirstNm;
}


@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

1 Answer 1

1

You need to deserialize to a class having as property an object of a class GetParentCommunications. That property needs to be marked with @JsonProperty("getParentCommunications").

On another note have only properties decorated with @JsonProperty, it's enough.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I am able to read it now. The pojos were auto-generated by jsonschema2pojo.org. I did not even pay attention to them.

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.