【问题标题】:Google Direction JSON to POJO (Jackson)谷歌方向 JSON 到 POJO(杰克逊)
【发布时间】:2016-03-07 02:34:32
【问题描述】:

问题:尝试将 JSON 从 http 请求(Google Directions)转换为 POJO(只是我自己的类)。我现在有“根”类“GoogleGeoCodeResponse”(GGCR 进一步在文本中,包含一些顶级字段,如状态、路由等),其中还有其他一些(路由包含此类和那个等)。我来了

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)

现在我完全陷入困境,任何想法都将不胜感激。 Getter 和 setter 仅针对 GGCR 定义,字段在 GGCR 中是私有的,在其他类中是公共的,gets/sets - public。

编辑: 主类:

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

【问题讨论】:

    标签: java json jackson google-directions-api


    【解决方案1】:

    您应该将@JsonIgnoreProperties(ignoreUnknown = true) 注释放在类的顶部。这是一个类级别的注释。

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class GoogleGeoCodeResponse {
    
    private String status;
    private GeocodedWaypoint[] geocodedWaypoints;
    private Route[] routes;
    
    ...
    

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

    使用注释

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

    或者配置你的 ObjectMapper:

    objectMapper.setPropertyNamingStrategy(
        PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
    

    我会使用映射器配置,因为您使用的 API 默认使用下划线作为字段名称。这样,您就可以避免使用 @JsonProperty 注释每个班级成员。

    【讨论】:

    • 如果我从快速测试中正确理解,属性不会一直下​​降,所以如果我需要忽略更深层次类中的某些字段,我需要重新定义它,对吗?
    • (通过进一步测试,我的假设似乎是正确的)。问题是,geocodedWaypoints 没有被分配(为空),这是否意味着(简单来说)Jackson lib.认为我的模型中没有来自 JSON 的 geocodedWaypoints 的位置,因此忽略它设置为 null?
    • 杰克逊似乎试图在您的对象中查找不存在的属性。您应该使用 @JsonProperty("geocode_waypoints") 注释您的 geocodeWaypoints 或配置您的 ObjectMapper 以将下划线转换为驼峰式大小写。我会更新我的答案。
    • 嗯,这似乎解决了问题,所以基本上你想遵循以下规则:要么以相同的方式命名字段,要么进行注释,要么在映射器中定义这些规则?并且提及要忽略的字段可能是个好主意?或者简单地说忽略未知就足够了?还是谢谢!
    • 是的,你有这三种方式,但是根据 Java 约定,你不能在你的类成员中(或任何地方)使用下划线。我用我的意见更新了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多