我想解析这个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);
答案 0 :(得分:1)
试试这个:
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Type listType = new TypeToken<List<OSRMAddress>>() {}.getType();
List<OSRMAddress> list = gson.fromJson(new InputStreamReader(inputStream), listType);
答案 1 :(得分:1)
从第一个和最后一个方向移除方括号并尝试。
所以你的字符串将如下所示。
{"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}