在JAVA中解析googleapi地址时出错

时间:2016-07-18 09:27:54

标签: java json google-maps geolocation gson

我想解析来自此网址的地址

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 个答案:

答案 0 :(得分:2)

您的回复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());
    }