【发布时间】:2017-07-01 15:40:25
【问题描述】:
我有一个 json:
{
"response": {
"GeoObjectCollection": {
"featureMember": [
{
"GeoObject": {
"description": "Country",
"name": "City",
"Point": {
"pos": "31.992615 45.057626"
}
}
},
{
"GeoObject": {
"description": "Country",
"name": "City",
"Point": {
"pos": "49.242414 49.895935"
}
}
}
]
}
}
}
我创建了 DTO:
GeographicCoordinateDto.java:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeographicCoordinateDto {
@JsonProperty("description")
private String location;
@JsonProperty("name")
private String cityName;
@JsonProperty("Point")
private GeographicCoordinatesDto geoCoordinates;
}
GeographicCoordinatesDto.java:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeographicCoordinatesDto {
@JsonProperty("pos")
private String geoCoordinates;
}
然后我得到JsonNode:
List<JsonNode> responseArrayOfObjects = mapper.readValue(new URL(yandexGeoCoderRestUrl+address), ObjectNode.class).findValues("GeoObject");
我正在尝试转换为我的DTO:
GeographicCoordinatesDto geo = mapper.convertValue(responseArrayOfObjects.get(0), GeographicCoordinatesDto.class);
但是,我有空对象:
GeographicCoordinatesDto(geoCoordinates=null)
可能出了什么问题?
更新:responseArrayOfObjects 包含:
【问题讨论】:
-
我会检查列表中的所有
JsonNodes。读取 JSON 时可能出错了?