繁体   English   中英

杰克逊反序列化GMaps JSON

[英]Jackson deserializing GMaps JSON

我正在尝试反序列化以下JSON: http : //maps.googleapis.com/maps/api/geocode/json? address= 04545-006

我的疑问是,如何以仅获得results.geometry.location.lat和results.geometry.location.lng的方式反序列化它?

由于我使用的是Spring,因此尝试使用RestTemplate并按如下方式注释帮助程序类,但未成功:

public class GmapsDto {

t@JsonProperty("results/geometry/location/lat")
private String lat;

@JsonProperty("results.geometry.location.lng")
private String lng;

public String getLat() {
    return lat;
}

public void setLat(String lat) {
    this.lat = lat;
}

public String getLng() {
    return lng;
}

public void setLng(String lng) {
    this.lng = lng;
}
}

有没有一种方法可以浏览对象树并仅获取我需要的信息?

谢谢。

Jackson JIRA拥有一张@JsonWrapped注解的票证,用于反序列化“平坦”对象(例如GMapsDto 不幸的是, 它已关闭 ,尚未在GitHub上的jackson-databind问题跟踪器中重新出现。

另外,您可以有多个结果 ,因为results实际上是一个JSON数组,而不是一个对象。

所以我看到了两条可能的路线。

  1. 要将Jackson用于自定义序列化/反序列化,请使用@JsonDeserialize注释您的类,并使用Jackson的Tree API实施自定义反序列化器。

    GMapsDto所有结果反序列化为GMapsDto实例:

     // in GMapsResult.java public class GMapsResult { private final List<GMapsDto> results; @JsonCreator public GMapsResult(@JsonProperty("results") List<GMapsDto> results) { this.results = results; } public List<GMapsDto> getResults() { return results; } } // in GMapsDto.java @JsonDeserialize(using = GMapsDto.Deserializer.class) public class GMapsDto { // ... private static final class Deserializer extends JsonDeserializer<GMapsDto> { @Override public GMapsDto deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode location = jp.readValueAsTree() .path("geometry") .path("location"); GMapsDto dto = new GMapsDto(); dto.setLat(location.get("lat").doubleValue()); dto.setLon(location.get("lat").doubleValue()); return dto; } } } } 
  2. @JsonCreator注释您的类,并创建伪造的内部类,它们表示JSON键resultsgeometrylocation ,例如:

     public final class GMapsDto { private final List<Results> results; @JsonCreator public GMapsDto(@JsonProperty("results") List<Results> results) {this.results = results;} public List<Result> getResults() { return results; } public static final class Result { private final Geometry geometry; @JsonCreator public Result(@JsonProperty("geometry") Geometry geometry) {this.geometry = geometry;} public Geometry getGeometry() { return geometry; } public static final class Geometry { private final Location location; @JsonCreator public Geometry(@JsonProperty("location") Location location) { this.location = location; } public Location getLocation() { return location; } public static final class Location { private final double lat; private final double lon; @JsonCreator public Geometry(@JsonProperty("lat") double lat, @JsonProperty("lon") double lon) { this.lat = lat; this.lon = lon; } public double getLat() { return lat; } public double getLon() { return lon; } } } } } 

    然后,您将为每个结果访问纬度和经度:

     for (Result result: dto.getResults()) { double {lat|lon} = result.getGeometry().getLocation().get{Lat(),Lon()}; } 

您希望能够通过注解使用简单的导航,对于@JsonWrapped注解已报告了JIRA问题,与@JsonUnwrapped相反,这基本上就是您在说的http://jira.codehaus.org/browse/杰克逊781 以我的拙见,最简单的方法是导航树并靠在JsonNode上,例如

    JsonNode rootNode = mapper.readValue(gmapJson, JsonNode.class); 
    System.out.println(rootNode.findValues("location"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM