2016-12-03 84 views
0

我需要解析Java對象中的Json並保存字段:destination_addresses,origin_addresses和duration。我無法獲得「持續時間」的值。這就是我要解析的JSON:從DistanceMatrix Google Api解析嵌套數組Json到Java,使用GSON

{ 
    "destination_addresses" : [ "Blocco Palma Primo, 95121 Fattoria Sole Delfino CT, Italia" ], 
    "origin_addresses" : [ 
     "Unnamed Road, 95121 Catania CT, Italia", 
     "Blocco Palma Primo, 95121 Fattoria Sole Delfino CT, Italia", 
     "Contrada Torre Allegra, 95121 Catania CT, Italia", 
     "Contrada Pantano d'Arci, Catania CT, Italia", 
     "Unnamed Road, 95121 Catania CT, Italia", 
     "Via Cassia, 95121 Catania CT, Italia", 
     "Contrada Pantano d'Arci, Catania CT, Italia", 
     "Contrada Pantano d'Arci, Catania CT, Italia", 
     "Contrada Pantano d'Arci, Catania CT, Italia", 
     "Contrada Pantano d'Arci, Catania CT, Italia" 
    ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "2,0 km", 
        "value" : 2037 
       }, 
       "duration" : { 
        "text" : "4 min", 
        "value" : 266 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "1 m", 
        "value" : 0 
       }, 
       "duration" : { 
        "text" : "1 min", 
        "value" : 0 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "3,8 km", 
        "value" : 3768 
       }, 
       "duration" : { 
        "text" : "7 min", 
        "value" : 400 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "5,3 km", 
        "value" : 5304 
       }, 
       "duration" : { 
        "text" : "6 min", 
        "value" : 374 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "8,2 km", 
        "value" : 8239 
       }, 
       "duration" : { 
        "text" : "13 min", 
        "value" : 785 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "11,5 km", 
        "value" : 11486 
       }, 
       "duration" : { 
        "text" : "15 min", 
        "value" : 901 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "12,2 km", 
        "value" : 12226 
       }, 
       "duration" : { 
        "text" : "18 min", 
        "value" : 1099 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "12,2 km", 
        "value" : 12226 
       }, 
       "duration" : { 
        "text" : "18 min", 
        "value" : 1099 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "12,2 km", 
        "value" : 12226 
       }, 
       "duration" : { 
        "text" : "18 min", 
        "value" : 1099 
       }, 
       "status" : "OK" 
      } 
     ] 
     }, 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "12,2 km", 
        "value" : 12226 
       }, 
       "duration" : { 
        "text" : "18 min", 
        "value" : 1099 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 

而這正是我試圖在Java中,得到我需要的字段:

public static void main(String[] args) throws Exception { 
     // TODO code application logic here 
     URL url = new URL(myUrl); 
     InputStreamReader reader = new InputStreamReader(url.openStream()); 
     SRD sr = new Gson().fromJson(reader, SRD.class); 

     System.out.println("destination: " + sr.destination_addresses.get(0)); 
     System.out.println("origins: " + sr.origin_addresses.get(2)); 
     System.out.println(sr.rows.get(2).elements.get(0).toString()); 



    } 

這些類:

private class SRD { 
     List<String> destination_addresses; 
     List<String> origin_addresses; 
     List<elements> rows; 

    } 


    private class elements { 
     List<duration> elements; 

    } 

    private class duration { 
     String text; 
     int value; 


     public String toString() { 
      return "duration{" + "text=" + text + ", value=" + value + '}'; 
     } 


    } 

執行此代碼我得到以下輸出:

destination: Blocco Palma Primo, 95121 Fattoria Sole Delfino CT, Italy

origins: Contrada Torre Allegra, 95121 Catania CT, Italy

duration{text=null, value=0}

顯然,我可以成功地解析字段destination_addresses和origin_addresses,但不是持續時間,其中給我值0和null。我錯在哪裏?我怎樣才能解決這個問題,並獲得持續時間的正確值(文本和值)?感謝您的幫助。

+0

問題是「元素」是一個字段持續時間(某些對象文本/值)的對象而不是持續時間列表。 http://www.jsonschema2pojo.org/可能會幫助你 – 2016-12-03 12:49:00

+0

如果我用「持續時間」替換我的班級「元素」的內容;和'System.out.println(sr.rows.get(2).duration.toString())'的main中的println;'' ,執行失敗,錯誤:線程「main」中的_Exception java.lang.NullPointerException \t at proveJson.SingleRouteParse.main(SingleRouteParse.java:35)_。我按照Netbeans的提示寫這個println。 – Ennio

+0

因爲在json中我使用了一個列表之前,元素後面跟着方括號[ – Ennio

回答

0

這裏,在java類名稱應該以大寫字母開頭。

如果是訪問對象的問題,只需定義getter方法。

我沒有做出獨立的持續時間和距離對象類,因爲它們都具有相同的類型和名稱屬性。

private class SRD { 
    List<String> destination_addresses; 
    List<String> origin_addresses; 
    List<Row> rows; 

} 

private class Row{ 
    List<Element> elements; 

} 


private class Element { 
    General duration; 
    General distance; 
    String status; 

} 

private class General { 
    String text; 
    int value; 


    public String toString() { 
     return "duration{" + "text=" + text + ", value=" + value + '}'; 
    } 


} 
+0

謝謝你,我已經解決了我的問題。我以類似於你關於你的方式修改課程,但沒有奏效。現在我會試着去理解爲什麼,但同時我解答了你的問題。 – Ennio

+0

不用客氣 – mismanc

+0

順便問一下這是什麼問題? – mismanc