在 JAVA 中解析 googleapi 地址时出错
Posted
技术标签:
【中文标题】在 JAVA 中解析 googleapi 地址时出错【英文标题】:Error parsing googleapi address in JAVA 【发布时间】:2016-07-18 09:27:53 【问题描述】:我想解析来自这个 URL 的地址
https://maps.googleapis.com/maps/api/geocode/json?address=Ctra.+Ibiza+a+San+Antonio%2C+Km+5%2C+07816+San+Rafael%2C+Illes+Balears%2C+Spain&key=YOUR_API_KEY
这个Json:
"results" : [ "address_components" : [ "long_name" : "Balearic Islands", "short_name" : "PM", "types" : [ "administrative_area_level_1", "political" ] , "long_name" : "Spain", "short_name" : "ES", "types" : [ "country", "political" ] ], "formatted_address" : "Balearic Islands, Spain", "geometry" : "bounds" : "northeast" : "lat" : 40.0945744, "lng" : 4.3277839 , "southwest" : "lat" : 38.6404833, "lng" : 1.1572405 , "location" : "lat" : 39.5341789, "lng" : 2.8577105 , "location_type" : "APPROXIMATE", "viewport" : "northeast" : "lat" : 39.9625326, "lng" : 3.4785808 , "southwest" : "lat" : 39.1207608, "lng" : 2.3031594 , "partial_match" : true, "place_id" : "ChIJV2Jp3FqSlxIRFU2l-yEDh_8", "types" : [ "administrative_area_level_1", "political" ] ], "status" : "OK"
还有这个类
public class Result
@SerializedName("address_components")
@Expose
private List<AddressComponent> addressComponents = new ArrayList<AddressComponent>();
@SerializedName("formatted_address")
@Expose
private String formattedAddress;
@SerializedName("geometry")
@Expose
private Geometry geometry;
@SerializedName("place_id")
@Expose
private String placeId;
@SerializedName("types")
@Expose
private List<String> types = new ArrayList<String>();
/**
*
* @return
* The addressComponents
*/
public List<AddressComponent> getAddressComponents()
return addressComponents;
/**
*
* @param addressComponents
* The address_components
*/
public void setAddressComponents(List<AddressComponent> addressComponents)
this.addressComponents = addressComponents;
/**
*
* @return
* The formattedAddress
*/
public String getFormattedAddress()
return formattedAddress;
/**
*
* @param formattedAddress
* The formatted_address
*/
public void setFormattedAddress(String formattedAddress)
this.formattedAddress = formattedAddress;
/**
*
* @return
* The geometry
*/
public Geometry getGeometry()
return geometry;
/**
*
* @param geometry
* The geometry
*/
public void setGeometry(Geometry geometry)
this.geometry = geometry;
/**
*
* @return
* The placeId
*/
public String getPlaceId()
return placeId;
/**
*
* @param placeId
* The place_id
*/
public void setPlaceId(String placeId)
this.placeId = placeId;
/**
*
* @return
* The types
*/
public List<String> getTypes()
return types;
/**
*
* @param types
* The types
*/
public void setTypes(List<String> types)
this.types = types;
和
public class AddressComponent
@SerializedName("long_name")
@Expose
private String longName;
@SerializedName("short_name")
@Expose
private String shortName;
@SerializedName("types")
@Expose
private List<String> types = new ArrayList<String>();
/**
*
* @return
* The longName
*/
public String getLongName()
return longName;
/**
*
* @param longName
* The long_name
*/
public void setLongName(String longName)
this.longName = longName;
/**
*
* @return
* The shortName
*/
public String getShortName()
return shortName;
/**
*
* @param shortName
* The short_name
*/
public void setShortName(String shortName)
this.shortName = shortName;
/**
*
* @return
* The types
*/
public List<String> getTypes()
return types;
/**
*
* @param types
* The types
*/
public void setTypes(List<String> types)
this.types = types;
...
我解析它
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
String jsonString = getGoogleMapsJsonString ();
Result result = gson.fromJson((jsonString), Result.class);
但result.getAddressComponents()
为空且result.getFormattedAddress()
为空
【问题讨论】:
【参考方案1】:您的响应 json 应反序列化为以下 dto:
public class Response
@SerializedName("results")
@Expose
private Collection<Result> results;
@SerializedName("address_components")
@Expose
private String status;
// getters/setter
你可以得到如下结果:
Response response = gson.fromJson((jsonString), Response.class);
Collection<Result> res = response.getResults();
for (Result result : res)
System.out.println(result.getAddressComponents());
System.out.println(result.getFormattedAddress());
【讨论】:
以上是关于在 JAVA 中解析 googleapi 地址时出错的主要内容,如果未能解决你的问题,请参考以下文章
尝试刷新 googleapis 的访问令牌时出现 invalid_grant 错误
stackoverfow访问 ajax.googleapis.com
Web Scraping:在 Python 中解析 JSON 时出现 KeyError