简体   繁体   中英

Java deserialization of JSON type object not working

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);
}

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM