2015-12-02 57 views
0

問題:嘗試將JSON從http請求(Google Directions)轉換爲POJO(僅僅是我自己的類)。我現在已經有了「根」類「GoogleGeoCodeResponse」(GGCR在文本中有更進一步的說明,其中包含一些頂級域如狀態,路線等),裏面還有一些其他的(路由包含這個類和那些等等)。嘗試使用ggcr = mapper.readValue(json, GoogleGeoCodeResponse.class);(JSON字符串是),即使在@JsonIgnoreProperties(ignoreUnknown = true)類GGCR轉換時,我得到Google Direction JSON to POJO(Jackson)

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: [email protected]; line: 2, column: 28] (through reference chain: Main.GoogleGeoCodeResponse.GoogleGeoCodeResponse["geocoded_waypoints"]) 

現在我完全卡住了,任何想法將不勝感激。 Getters和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

你應該把@JsonIgnoreProperties(ignoreUnknown = true)標註在頂部用CLA SS。這是一個課程級別的註釋。

@JsonIgnoreProperties(ignoreUnknown = true) 
public class GoogleGeoCodeResponse { 

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

... 

就你而言,似乎傑克遜試圖將JSON字段映射到不存在的屬性。您可以通過兩種不同的方式解決這個問題:

使用annotattion

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

或配置您的ObjectMapper:

objectMapper.setPropertyNamingStrategy(
    PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); 

我會映射配置去,因爲你正在使用使用API​​用下劃線字段名稱的缺省值。通過這種方式,您可以避免使用@JsonProperty註釋每個班級成員。

+0

如果我從快速測試中正確理解,屬性不會一直下​​降,所以如果我需要忽略更深層次類中的某些字段,我需要再次定義它,請更正? –

+0

(我的假設似乎是正確的進一步測試)。問題是,geocodedWaypoints沒有被分配(是空的),這是否意味着(簡單地說)傑克遜lib。認爲在我的模型中沒有地方從JSON的geocodedWaypoints,因此忽略它設置爲空? –

+0

看來,傑克遜正試圖在你的對象中找到一個不存在的屬性。你應該用@JsonProperty(「geocode_waypoints」)註釋你的'geocodeWaypoints',或者配置你的'ObjectMapper'來將下劃線轉換爲駱駝大小寫。我會更新我的答案。 – Philipe