2013-12-20 45 views
0

我與後續的JSON對象的工作,我從谷歌獲取Places API的獲得2個值由大JSON對象

{ 

    "debug_info":[ 
    ], 
    "html_attributions":[ 
    ], 
    "result":{ 
     "address_components":[ 
      { 
       "long_name":"109", 
       "short_name":"109", 
       "types":[ 
        "street_number" 
       ] 
      }, 
      { 
       "long_name":"Torstraße", 
       "short_name":"Torstraße", 
       "types":[ 
        "route" 
       ] 
      }, 
      { 
       "long_name":"Mitte", 
       "short_name":"Mitte", 
       "types":[ 
        "sublocality", 
        "political" 
       ] 
      }, 
      { 
       "long_name":"Mitte", 
       "short_name":"Mitte", 
       "types":[ 
        "sublocality", 
        "political" 
       ] 
      }, 
      { 
       "long_name":"Berlin", 
       "short_name":"Berlin", 
       "types":[ 
        "locality", 
        "political" 
       ] 
      }, 
      { 
       "long_name":"Berlin", 
       "short_name":"Berlin", 
       "types":[ 
        "administrative_area_level_1", 
        "political" 
       ] 
      }, 
      { 
       "long_name":"Germany", 
       "short_name":"DE", 
       "types":[ 
        "country", 
        "political" 
       ] 
      }, 
      { 
       "long_name":"10119", 
       "short_name":"10119", 
       "types":[ 
        "postal_code" 
       ] 
      } 
     ], 
     "adr_address":"\u003cspanclass=\"street-address\"\u003eTorstraße109\u003c/span\u003e,\u003cspanclass=\"postal-code\"\u003e10119\u003c/span\u003e\u003cspanclass=\"locality\"\u003eBerlin\u003c/span\u003e,\u003cspanclass=\"country-name\"\u003eGermany\u003c/span\u003e", 
     "formatted_address":"Torstraße109,10119Berlin,Germany", 
     "geometry":{ 
      "location":{ 
       "lat":52.5300431, 
       "lng":13.40297 
      } 
     }, 
     "icon":"http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png", 
     "id":"18c00ced59b4a7fb929dde10a01169517666eec5", 
     "name":"Torstraße109", 
     "reference":"CpQBiQAAAGrYOH9GhUIR5_9XJm8YPZYoudNVoYeIWwD1-zFzjdUp2eKujIt85bUM78FWiY9OgGm2pPoxnCjE5EMhXNz9hiPcLLybacpdSQ10x0mO8UQNs1Mj-EyjGMfBaowMSAxeye_2aDvCyJEk5JAkzTkqXenGi60Dx24o5zKTH1nt1yDBJ3BlKb7Uaas6dBTz2GoqKBIQNFg_NmrCnRGgLxw5VEvtghoU6fl4owiC-5mNZ5RCjS976-S1fyQ", 
     "types":[ 
      "street_address" 
     ], 
     "url":"https://maps.google.com/maps/place?q=Torstra%C3%9Fe+109,+10119+Berlin,+Germany&ftid=0x47a851e486a78201:0xec18bf6093c5c499", 
     "vicinity":"Mitte" 
    }, 
    "status":"OK" 

} 

,我想拉的唯一場所,它是緯度,經度和formated_address 。

使用傑克森最有效的方法是什麼?

我知道我可以編寫一個自定義的反序列化器,但是之後我必須多次調用jsonParser.nextToken。

什麼是更好的方式去解決這個問題?

編輯

我能拿出一個目前最好的是這個

public class LocationDeserializer extends JsonDeserializer<Location> { 
    @Override 
    public Location deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { 
     Location location = new Location(); 

     int len = 100; 
     for (int i = 0; i < len; ++i) { 
      String currentName = jsonParser.getCurrentName(); 
      if (currentName != null) { 
       if (jsonParser.getCurrentName().equals("formatted_address")) { 
        location.setDescription(jsonParser.getText()); 
       } else if (jsonParser.getCurrentName().equals("lat")) { 
        location.setLatitude(jsonParser.getDoubleValue()); 
       } else if (jsonParser.getCurrentName().equals("lng")) { 
        location.setLongitude(jsonParser.getDoubleValue()); 
        break; 
       } 
      } 
      jsonParser.nextValue(); 
     } 
     return location; 
    } 
} 

回答

-1

你爲什麼不拿起由它的價值的鑰匙?

JSONObject json = new JSONObject(response); 

答覆是您從Google收到的回覆。

然後讓你的價值觀:

JSONObject address = json.getJSONObject("result"); 
String formattedAddress = address.getString("formatted_address"); 


JSONObject locationData = address.getJSONObject("geometry").getJSONObject("location"); 
Location location = new Location(); 
location.setLatitude(locationData.getDouble("lat")); 
location.setLongitude(locationData.getDouble("lng")); 

需要導入

import org.json.JSONObject; 
+0

好吧,我用傑克遜庫等反序列化/序列化和它運作良好。我認爲這是一致的好。另外,我可以用下面的行來減少大量的鍋爐板代碼: Location location = objectMapper.readValue(httpURLConnection.getInputStream(),Location.class); 不管JSONObject是否必須迭代對象? – jiduvah

+0

顯然,您可以調整我上面提供給您正在使用的Jackson Library的內容。我不熟悉它,所以我不能告訴你如何,但想法是相同的,以獲得持有的特定JSON節點訪問值,而不是遍歷整個JSON響應 – Lefteris

+0

你怎麼知道如果你不清楚圖書館是否明顯? – jiduvah