JSON类型对象的Java反序列化不起作用
这是传递给我的数据。我无法控制此数据的结构:JSON类型对象的Java反序列化不起作用,java,json,deserialization,Java,Json,Deserialization,这是传递给我的数据。我无法控制此数据的结构: { "getParentCommunications":{ "ccList":[ { "personId":12, "parentFirstNm":"johnny" }, { "personId&qu
{
"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()); //(produces a java.lang.NullPointerException)
ClientResponse-response=webResource.post(ClientResponse.class,输入);
ObjectMapper mapper=新的ObjectMapper();
configure(在未知属性上反序列化feature.FAIL,false);
GetParentCommunications gpc=mapper.readValue(response.getEntity(String.class)、GetParentCommunications.class);
List-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);
}
}
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder({
“ccList”
})
公共类GetParentCommunications实现可序列化
{
@JsonProperty(“ccList”)
私有列表ccList=null;
@杰索尼奥雷
私有映射additionalProperties=new HashMap();
私有最终静态长serialVersionUID=8422191816885827338L;
@JsonProperty(“ccList”)
公共列表getCcList(){
返回CCL列表;
}
@JsonProperty(“ccList”)
公共作废集合列表(列表集合列表){
this.ccList=ccList;
}
@JsonAnyGetter
公共映射getAdditionalProperties(){
返回此。附加属性;
}
@JSONANYSETER
public void setAdditionalProperty(字符串名称、对象值){
this.additionalProperties.put(名称、值);
}
}
和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);
}
}
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder({
“人格”,
“parentFirstNm”
})
公共类列表{
@JsonProperty(“personId”)
私有整数personId;
@JsonProperty(“parentFirstNm”)
私有字符串parentFirstNm;
@杰索尼奥雷
私有映射additionalProperties=new HashMap();
@JsonProperty(“personId”)
公共整数getPersonId(){
回归人格;
}
@JsonProperty(“personId”)
public void setPersonId(整数personId){
this.personId=personId;
}
@JsonProperty(“parentFirstNm”)
公共字符串getParentFirstNm(){
返回parentFirstNm;
}
@JsonProperty(“parentFirstNm”)
public void setParentFirstNm(字符串parentFirstNm){
this.parentFirstNm=parentFirstNm;
}
@JsonAnyGetter
公共映射getAdditionalProperties(){
返回此。附加属性;
}
@JSONANYSETER
public void setAdditionalProperty(字符串名称、对象值){
this.additionalProperties.put(名称、值);
}
}
您需要反序列化到具有GetParentCommunications类的对象作为属性的类。该属性需要用@JsonProperty(“getParentCommunications”)标记
另一方面,我们只有用@JsonProperty装饰的属性,这就足够了。谢谢!我现在能读了。POJO由自动生成。我甚至没有注意到他们。