我在使用谷歌地理编码 api 时遇到问题。例如,使用此网址http://maps.google.com/maps/api/geocode/json?latlng=47.3195254,5.0430687&sensor=true,我想用杰克逊 2 解析 json 以创建 POJO。
所以我的课是
public class GeocoderResult {
@JsonProperty("results") private List<GeocoderGoog> geocoder;
@JsonProperty("status") private String status;
public List<GeocoderGoog> getGeocoder() {
return geocoder;
}
public String getStatus() {
return status;
}
}
为了反序列化 json,我使用
HttpURLConnection connection = (HttpURLConnection) new URL(baseUrl).openConnection();
ObjectMapper mapper = new ObjectMapper();
// disable exceptions when there is unknown properties
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
int statusCode = connection.getResponseCode();
Log.d(Constants.D_TAG, "Status : "+statusCode);
if (statusCode == HttpURLConnection.HTTP_OK) { // 200
InputStream is = new BufferedInputStream(connection.getInputStream());
status = (Status) mapper.readValue(is, GeocoderResult.class);
}
我有以下错误:
09:38:42.737 Thread-24889 An exception occurred during request network execution :Unexpected close marker '}': expected ']' (for ROOT starting at [Source: java.io.BufferedInputStream@428a1840; line: 1, column: 0])
at [Source: java.io.BufferedInputStream@428a1840; line: 2, column: 14]
com.fasterxml.jackson.core.JsonParseException: Unexpected close marker '}': expected ']' (for ROOT starting at [Source: java.io.BufferedInputStream@428a1840; line: 1, column: 0])
我不明白问题出在哪里...
ps:我使用 jackson-core、jackson-databind 和 jackson-annotations 2.1.4