Search code examples
javajsongoogle-mapsgeolocationgson

Error parsing googleapi address in JAVA


I want to parse the address that comes from this 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

This 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" }

and this classes

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;
    }

}

and

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;
    }

}

...

I parse it

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();

String jsonString = getGoogleMapsJsonString ();
Result result = gson.fromJson((jsonString), Result.class);  

But result.getAddressComponents() is empty and result.getFormattedAddress() is null


Solution

  • Your response json should be deserialized into following dto:

    public class Response {
        @SerializedName("results")
        @Expose
        private Collection<Result> results;
        @SerializedName("address_components")
        @Expose
        private String status;
        // getters/setter
    }
    

    And than you can get result like:

        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());
        }