当前位置: 首页 > 知识库问答 >
问题:

谷歌定向JSON到POJO(杰克逊)

任云瀚
2023-03-14

问题:试图将JSON从http请求(Google方向)转换为POJO(只是我自己的类)。我现在已经有了“根”类“GoogleGeoCodeResponse”(文本中的GGCR,包含一些顶级字段,如status、routes等),其中还有一些其他字段(routes包含这个类和那个类,等等)。我要走了

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中是私有的,在其他类中是公共的,get/set-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


共有1个答案

燕嘉熙
2023-03-14

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

@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);

我会选择mapper配置,因为您使用的API默认情况下使用下划线作为字段名称。通过这种方式,您可以避免使用@JsonProperty注释每个类成员。

 类似资料:
  • 问题内容: 我知道如果文件User.json有 我可以像这样构造一个User对象: 但是,如果文件User.json具有以下内容,该如何构造对象列表: ? 问题答案: 多种方式:如果您具有这些的JSON数组,则可以执行以下操作: 或者,如果只是一系列根级别值,则可以执行以下操作: 并遍历值(添加到列表等)

  • 我如何告诉Jackson忽略JSON名称? 我有以下POJO: 当我有这样的东西: “ABCName”:“foo”,然后杰克逊没有认出它抛出错误。 它期望的是: “abcName”:“foo”。 代码: 输出:{"abcname":"Foo"} 然后我试着用@JsonProperty(“ABCName”)注释ABCName 在我注释并运行代码之后,我得到的是:{“ABCName”:“Foo”,“A

  • 问题内容: 我正在使用Jackson将应用程序的REST接口序列化为JSON表示形式的POJO域对象。我想为某些类型自定义序列化,以将其他属性添加到POJO中不存在的JSON表示中(例如,添加一些元数据,参考数据等)。我知道如何编写自己的方法,但是在那种情况下,我需要为对象的 每个* 属性显式调用方法,而我所需要的只是 添加 一个附加属性。换句话说,我希望能够编写如下内容: *** 或者(甚至更好

  • 问题内容: 我需要从Java pojo创建这样的Json有效负载: 所以我创建了一个Java pojo像这样: 我喜欢将Label类中的“ add”属性设置为动态,以便它也可以采用诸如“ remove”,“ set”之类的值。除了为每个人创建另一个POJO之外,还有其他方法吗? 问题答案: 您可以使用JsonAnyGetter批注来创建动态对。下面的示例显示了如何为同一类生成3个不同的键: 上面的

  • 我还在学习如何使用Jackson。。。 所以我有一个JSON对象,它的值有时是整数、长字符串或列表 值:整数 值:字符串 价值:列表 所以总的来说。。。 这是我的POJO模型 这是我的映射器代码 问题是,当我执行代码时,出现以下错误: 我很清楚,之所以会发生这种情况,是因为“value”可以包含三种不同类型中的一种,我如何使代码足够灵活以适应这些类型。。。我总是可以在其他方法中检测值是int、Li

  • 有没有办法让Jackson序列化某个流对象(并在之后关闭)?这样地: 使现代化 澄清:我想流式传输内容,而不仅仅是将其序列化到单个String对象。