Google 前往 POJO(杰克逊)的方向 JSON

Google Direction JSON to POJO (Jackson)

问题:尝试将 JSON 从 http 请求(Google 方向)转换为 POJO(只是我自己的 classes)。我现在有 "root" class "GoogleGeoCodeResponse" (GGCR 在文本中更进一步,包含一些顶级字段,如状态、路由等),里面还有一些其他的(路由包含这个class 等等)。我正在

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "geocoded_waypoints" (class Main.GoogleGeoCodeResponse.GoogleGeoCodeResponse), not marked as ignorable (3 known properties: "status", "geocodedWaypoints", "routes"])
 at [Source: java.io.StringReader@1717824; line: 2, column: 28] (through reference chain: Main.GoogleGeoCodeResponse.GoogleGeoCodeResponse["geocoded_waypoints"])

尝试使用 ggcr = mapper.readValue(json, GoogleGeoCodeResponse.class); 进行转换时(json 是字符串),即使在 GGCR @JsonIgnoreProperties(ignoreUnknown = true) class.

到现在为止我完全卡住了,任何想法都将不胜感激。 getter 和 setter 仅为 GGCR 定义,字段在 GGCR 中是私有的,在其他 classes 中是 public,gets/sets - public.

编辑: 主要 class:

public class Main {
public static void main(String[] args) {
    String json="";
    String origin = "Manhattan+New+York+USA";
    String destination = "Newark+New+YorkUSA";
    String mode="bicycling";
    try {
        URL url = new URL("https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+"&mode="+mode);
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            json+=inputLine+"\n";
        }
        in.close();
    }
    catch (Exception e)
    {
        System.out.println(e.toString());
    }

    GoogleGeoCodeResponse ggcr=null;
    ObjectMapper mapper = new ObjectMapper();
    try {
        ggcr = mapper.readValue(json, GoogleGeoCodeResponse.class);
    }
    catch(Exception e){
        System.out.println(e.toString());
    }
    if (ggcr!=null)
    {
        System.out.println("Status: " + ggcr.getStatus());
    }
}

或此处http://pastebin.com/pEeqn4f2

GGCR:

public class GoogleGeoCodeResponse {

@JsonIgnoreProperties(ignoreUnknown = true)
private String status;
private GeocodedWaypoint[] geocodedWaypoints;
private Route[] routes;
public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public GeocodedWaypoint[] getGeocodedWaypoints() {
    return geocodedWaypoints;
}

public void setGeocodedWaypoints(GeocodedWaypoint[] geocodedWaypoints) {
    this.geocodedWaypoints = geocodedWaypoints;
}

public Route[] getRoutes() {
    return routes;
}

public void setRoutes(Route[] routes) {
    this.routes = routes;
}

或此处http://pastebin.com/Fs5DYPSY

您应该将 @JsonIgnoreProperties(ignoreUnknown = true) 注释放在 class 的顶部。是class级注解。

@JsonIgnoreProperties(ignoreUnknown = true)
public class GoogleGeoCodeResponse {

private String status;
private GeocodedWaypoint[] geocodedWaypoints;
private Route[] routes;

...

在您的情况下,Jackson 似乎试图将 JSON 字段映射到不存在的 属性。您可以通过两种不同的方式修复它:

使用注释

@JsonProperty("geocoded_waypoints")
private GeocodedWaypoint[] geocodedWaypoints;

或配置您的 ObjectMapper:

objectMapper.setPropertyNamingStrategy(
    PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);

我会使用映射器配置,因为您使用的 API 默认情况下对字段名称使用下划线。通过这种方式,您可以避免使用 @JsonProperty.

注释每个 class 成员