Java反序列化JSON类型对象不起作用

huangapple go评论73阅读模式
英文:

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:

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

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&lt;CcList&gt; cclist = gpc.getCcList();
System.out.println( &quot;size &quot; + 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({
&quot;ccList&quot;
})
public class GetParentCommunications implements Serializable
{

@JsonProperty(&quot;ccList&quot;)
private List&lt;CcList&gt; ccList = null;

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

@JsonProperty(&quot;ccList&quot;)
public List&lt;CcList&gt; getCcList() {
return ccList;
}

@JsonProperty(&quot;ccList&quot;)
public void setCcList(List&lt;CcList&gt; ccList) {
this.ccList = ccList;
}

@JsonAnyGetter
public Map&lt;String, Object&gt; 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({
&quot;personId&quot;,
&quot;parentFirstNm&quot;
})
public class CcList {

@JsonProperty(&quot;personId&quot;)
private Integer personId;
@JsonProperty(&quot;parentFirstNm&quot;)
private String parentFirstNm;

@JsonIgnore
private Map&lt;String, Object&gt; additionalProperties = new HashMap&lt;String, Object&gt;();

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

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

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

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


@JsonAnyGetter
public Map&lt;String, Object&gt; 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.

huangapple
  • 本文由 发表于 2020年9月12日 06:52:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63855242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定