gpt4 book ai didi

java - Google 方向 JSON 到 POJO ( jackson )

转载 作者:行者123 更新时间:2023-12-01 10:48:26 25 4
gpt4 key购买 nike

问题:尝试将 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是字符串)进行转换时,即使使用@JsonIgnoreProperties(ignoreUnknown = true) GGCR 类。

现在我完全陷入困境,任何想法将不胜感激。 getter 和 setter 仅为 GGCR 定义,字段在 GGCR 中是私有(private)的,在其他类中是公共(public)的,获取/设置 - 公共(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

最佳答案

您应该将 @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);

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

关于java - Google 方向 JSON 到 POJO ( jackson ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34049738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com