gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:32:15 25 4
gpt4 key购买 nike

我想解析这个 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: 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);

最佳答案

删除第一个和最后一个方括号并尝试。

所以你的字符串将如下所示。

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

关于java - 使用 GSON : JsonSyntaxException: java. lang.IllegalStateException 进行 Json 解析:预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com