我有一个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
包含:
您正在尝试pos
从GeographicCoordinatesDto
对象获取信息,但它位于的Point
对象内部GeographicCoordinatesDto
。
您可以改为:
List<JsonNode> responseArrayOfObjects = mapper.readValue(new URL(yandexGeoCoderRestUrl+address), ObjectNode.class).findValues("Point");
或为Point创建另一个类:
@JsonIgnoreProperties(ignoreUnknown = true)
class Point {
@JsonProperty("pos")
private String geoCoordinates;
}
并用于GeographicCoordinatesDto
:
@JsonIgnoreProperties(ignoreUnknown = true)
class GeographicCoordinatesDto {
@JsonProperty("Point")
private Point point;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句