使用GSON进行Json解析:JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径处为BEGIN_ARRAY

Nunyet de CanCalçada

我想解析这个Json响应: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}]

使用Gson,因此根据响应创建了这个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;
    }

}

但是,当我解析它时,出现了一个错误(com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY)

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();           
gson.fromJson(new InputStreamReader(inputStream), OSRMAddress.class);
维杰·米斯特里(Vijay Mistry)

从头和尾上移开方括号,然后尝试。

因此,您的字符串将如下所示。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档