【发布时间】: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;
}
【问题讨论】:
标签: java json jackson google-directions-api