Java 将JSON嵌套到POJO
我试图调用一个外部RESTful web服务,需要解析嵌套JSON附带的响应。我试图解析出以下内容: {“列表”:[{“列表id”:“abcdef”,“列表名称”:“testing123”},{“列表id”:“xyz”,“列表名称”:“testing456”}]} 我的POJO类如下所示:Java 将JSON嵌套到POJO,java,json,spring,jackson,Java,Json,Spring,Jackson,我试图调用一个外部RESTful web服务,需要解析嵌套JSON附带的响应。我试图解析出以下内容: {“列表”:[{“列表id”:“abcdef”,“列表名称”:“testing123”},{“列表id”:“xyz”,“列表名称”:“testing456”}]} 我的POJO类如下所示: @JsonIgnoreProperties(ignoreUnknown = true) public class ListGetResponse { private String list = "";
@JsonIgnoreProperties(ignoreUnknown = true)
public class ListGetResponse {
private String list = "";
private SMSList lists;
public String getList() {
return list;
}
public void setList(String list) {
this.list = list;
}
public SMSList getLists() {
return lists;
}
public void setLists(SMSList lists) {
this.lists = lists;
}
@Override
public String toString() {
return "SMSListGetResponse [list=" + list + "]";
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class SMSList {
private String list = "";
private String list_id = "";
private String list_name = "";
public String getList() {
return list;
}
public void setList(String list) {
this.list = list;
}
public String getList_id() {
return list_id;
}
public void setList_id(String list_id) {
this.list_id = list_id;
}
public String getList_name() {
return list_name;
}
public void setList_name(String list_name) {
this.list_name = list_name;
}
@Override
public String toString() {
return "SMSList [list_id=" + list_id + ", list_name=" + list_name
+ "]";
}
}
我通过这个方法调用这个函数
public void get()
{
RestTemplate restTemplate = SMSRestFactory.getRestTemplate();
ResponseEntity<ListGetResponse> responseEntity = restTemplate.getForEntity(SMS_LIST_GET, ListGetResponse.class);
ListGetResponse body = responseEntity.getBody();
System.out.println(responseEntity.getBody());
System.out.println(body);
}
让它发挥作用。将我的SMSList对象更改为列表
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No suitable constructor found for type [simple type, class com.em.ustms.sms.list.model.ListGetResponse$SMSList]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1d1ab77; line: 1, column: 11] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.em.ustms.sms.list.model.ListGetResponse$SMSList]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1d1ab77; line: 1, column: 11] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:788)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:773)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:553)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:268)
at com.em.ustms.sms.service.SMSListService.get(SMSListService.java:59)
at com.em.ustms.sms.Application.main(Application.java:12)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.em.ustms.sms.list.model.ListGetResponse$SMSList]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1d1ab77; line: 1, column: 11] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1078)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:268)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:225)
... 9 more
通过将SMSClass更改为静态类解决了此问题:
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ListGetResponse {
private List<SMSList> list;
public List<SMSList> getList() {
return list;
}
public void setList(List<SMSList> list) {
this.list = list;
}
@Override
public String toString() {
return "SMSListGetResponse [list=" + list + "]";
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class SMSList {
private String list_id = "";
private String list_name = "";
public String getList_id() {
return list_id;
}
public void setList_id(String list_id) {
this.list_id = list_id;
}
public String getList_name() {
return list_name;
}
public void setList_name(String list_name) {
this.list_name = list_name;
}
@Override
public String toString() {
return "SMSList [list_id=" + list_id + ", list_name=" + list_name
+ "]";
}
}
}
import java.util.List;
导入com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
公共类ListGetResponse{
私人名单;
公共列表getList(){
退货清单;
}
公共无效集合列表(列表){
this.list=列表;
}
@凌驾
公共字符串toString(){
返回“SMSListGetResponse[list=“+list+”]”;
}
@JsonIgnoreProperties(ignoreUnknown=true)
公共静态类SMSList{
私有字符串列表_id=“”;
私有字符串列表_name=“”;
公共字符串getList_id(){
返回列表\u id;
}
公共无效集合列表id(字符串列表id){
this.list\u id=list\u id;
}
公共字符串getList_name(){
返回列表名称;
}
公共无效集合列表名称(字符串列表名称){
this.list\u name=list\u name;
}
@凌驾
公共字符串toString(){
返回“SMSList[list_id=“+list_id+”,list_name=“+list_name
+ "]";
}
}
}
您的响应对象应具有列表类型的列表字段。看看Jackson数据绑定手册:这不是“嵌套JSON”,而是普通的JSON。“嵌套JSON”是指存在编码的JSON字符串作为解码的JSON字符串的元素。
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ListGetResponse {
private List<SMSList> list;
public List<SMSList> getList() {
return list;
}
public void setList(List<SMSList> list) {
this.list = list;
}
@Override
public String toString() {
return "SMSListGetResponse [list=" + list + "]";
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class SMSList {
private String list_id = "";
private String list_name = "";
public String getList_id() {
return list_id;
}
public void setList_id(String list_id) {
this.list_id = list_id;
}
public String getList_name() {
return list_name;
}
public void setList_name(String list_name) {
this.list_name = list_name;
}
@Override
public String toString() {
return "SMSList [list_id=" + list_id + ", list_name=" + list_name
+ "]";
}
}
}