使用 Jackson 将 ObjectNode 转换为 Java 对象
Convert ObjectNode to Java Object using Jackson
我有一个 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
包含:
您正试图从 GeographicCoordinatesDto
对象中获取 pos
,但它位于 GeographicCoordinatesDto
的 Point
对象中。
您可以这样做:
List<JsonNode> responseArrayOfObjects = mapper.readValue(new URL(yandexGeoCoderRestUrl+address), ObjectNode.class).findValues("Point");
或为点创建另一个 class:
@JsonIgnoreProperties(ignoreUnknown = true)
class Point {
@JsonProperty("pos")
private String geoCoordinates;
}
并在GeographicCoordinatesDto
中使用它:
@JsonIgnoreProperties(ignoreUnknown = true)
class GeographicCoordinatesDto {
@JsonProperty("Point")
private Point point;
}
我有一个 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
包含:
您正试图从 GeographicCoordinatesDto
对象中获取 pos
,但它位于 GeographicCoordinatesDto
的 Point
对象中。
您可以这样做:
List<JsonNode> responseArrayOfObjects = mapper.readValue(new URL(yandexGeoCoderRestUrl+address), ObjectNode.class).findValues("Point");
或为点创建另一个 class:
@JsonIgnoreProperties(ignoreUnknown = true)
class Point {
@JsonProperty("pos")
private String geoCoordinates;
}
并在GeographicCoordinatesDto
中使用它:
@JsonIgnoreProperties(ignoreUnknown = true)
class GeographicCoordinatesDto {
@JsonProperty("Point")
private Point point;
}