英文:
Java deserialization of JSON type object not working
问题
这是传递给我的数据。我无法控制这些数据的结构:
{
"getParentCommunications":{
"ccList":[
{
"personId":12,
"parentFirstNm":"johnny"
},
{
"personId":14,
"parentFirstNm":"Sue"
},
{
"personId":19,
"parentFirstNm":"Ashley"
}
]
}
}
我想要能够获取ccList数据并查看其中的信息。我正试图将数据放入一个Java类中,以便根据需要进行操作。
使用Jackson,我有以下代码:
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()); //(产生java.lang.NullPointerException)
最后,这是我认为可以获取我想要的数据的两个类。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);
}
}
和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);
}
}
英文:
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
得分: 1
你需要反序列化为一个具有一个名为GetParentCommunications的类的属性的类。该属性需要用@JsonProperty("getParentCommunications")进行标记。
另外注意,只有用@JsonProperty装饰的属性就足够了。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论