Search code examples
javajsongeolocationgson

Json parse using GSON : JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $


I want to parse this Json response: http://nominatim.openstreetmap.org/search?format=json&q=42+rue+de+la+Folie-M%C3%A9ricourt+11e+Paris

[{"place_id":"14004695","licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"node","osm_id":"1292272363","boundingbox":["48.8634698","48.8635698","2.3724866","2.3725866"],"lat":"48.8635198","lon":"2.3725366","display_name":"42, Rue de la Folie-Méricourt, St-Ambroise, 11th Arrondissement, Paris, Ile-de-France, Metropolitan France, 75011, France","class":"place","type":"house","importance":0.611}]

using Gson, so based in the response I created this bean:

import java.util.ArrayList;
import java.util.List;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class OSRMAddress {

    @SerializedName("place_id")
    @Expose
    private String placeId;
    @Expose
    private String licence;
    @SerializedName("osm_type")
    @Expose
    private String osmType;
    @SerializedName("osm_id")
    @Expose
    private String osmId;
    @Expose
    private List<String> boundingbox = new ArrayList<String>();
    @Expose
    private Double lat;
    @Expose
    private Double lon;

    @SerializedName("display_name")
    @Expose
    private String displayName;
    @SerializedName("class")
    @Expose
    private String _class;
    @Expose
    private String type;
    @Expose
    private Double importance;
    public String getPlaceId() {
        return placeId;
    }
    public void setPlaceId(String placeId) {
        this.placeId = placeId;
    }
    public String getLicence() {
        return licence;
    }
    public void setLicence(String licence) {
        this.licence = licence;
    }
    public String getOsmType() {
        return osmType;
    }
    public void setOsmType(String osmType) {
        this.osmType = osmType;
    }
    public String getOsmId() {
        return osmId;
    }
    public void setOsmId(String osmId) {
        this.osmId = osmId;
    }
    public List<String> getBoundingbox() {
        return boundingbox;
    }
    public void setBoundingbox(List<String> boundingbox) {
        this.boundingbox = boundingbox;
    }   
    public Double getLat() {
        return lat;
    }
    public void setLat(Double lat) {
        this.lat = lat;
    }
    public Double getLon() {
        return lon;
    }
    public void setLon(Double lon) {
        this.lon = lon;
    }
    public String getDisplayName() {
        return displayName;
    }
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
    public String get_class() {
        return _class;
    }
    public void set_class(String _class) {
        this._class = _class;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Double getImportance() {
        return importance;
    }
    public void setImportance(Double importance) {
        this.importance = importance;
    }

}

But when I parse it, I got an error (com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $)

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();           
gson.fromJson(new InputStreamReader(inputStream), OSRMAddress.class);

Solution

  • Remove square bracket from first and last and try .

    So your string will be like below .

    {"place_id":"14004695","licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"node","osm_id":"1292272363","boundingbox":["48.8634698","48.8635698","2.3724866","2.3725866"],"lat":"48.8635198","lon":"2.3725366","display_name":"42, Rue de la Folie-Méricourt, St-Ambroise, 11th Arrondissement, Paris, Ile-de-France, Metropolitan France, 75011, France","class":"place","type":"house","importance":0.611}