Java Spring JSON解析错误:带有数组的JSON解析错误。为什么?
我正在尝试获取的JSON格式:Java Spring JSON解析错误:带有数组的JSON解析错误。为什么?,java,json,spring,jackson,Java,Json,Spring,Jackson,我正在尝试获取的JSON格式: [ { “id”:“110”, “姓名”:“英格兰”, “区域”:[ { “id”:“1620”, “名称”:“伦敦” }, ... ] }, ... ] dto类别: @数据 公共级WorldDTO{ 私人名单国家; } @资料 私有静态类CountryDTO{ @JsonProperty(value=“id”) 私有字符串id; @JsonProperty(value=“name”) 私有字符串名称; @JsonProperty(value=“areas”)
[
{
“id”:“110”,
“姓名”:“英格兰”,
“区域”:[
{
“id”:“1620”,
“名称”:“伦敦”
},
...
]
},
...
]
dto类别:
@数据
公共级WorldDTO{
私人名单国家;
}
@资料
私有静态类CountryDTO{
@JsonProperty(value=“id”)
私有字符串id;
@JsonProperty(value=“name”)
私有字符串名称;
@JsonProperty(value=“areas”)
私人名单领域;
}
@资料
公共类区域{
@JsonProperty(value=“id”)
私有字符串id;
@JsonProperty(value=“name”)
私有字符串名称;
}
我调用API的一段代码:
RequestEntity request=newrequestentity(HttpMethod.GET,uri);
ResponseEntity response=restemplate.exchange(请求,新的参数化TypeReference(){});
WorldDTO dto=response.getBody();
在我看来,由于字段private列出了国家,因此可能会发生错误代码>(WorldDTO.class)没有@JsonProperty
注释。但是json中也没有这个对象的名称
我做错了什么?你不需要worlddt来上课
它认为您正在尝试解析一个对象
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.radar.dto.WorldDTO] and content type [application/json;charset=UTF-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot deserialize instance of `com.radar.dto.WorldDTO` out of START_ARRAY token;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.radar.dto.WorldDTO` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
您应该使用响应属性
您不需要WorldDTO
类
它认为您正在尝试解析一个对象
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.radar.dto.WorldDTO] and content type [application/json;charset=UTF-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot deserialize instance of `com.radar.dto.WorldDTO` out of START_ARRAY token;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.radar.dto.WorldDTO` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
您应该使用响应属性