gpt4 book ai didi

android - jackson 2 反序列化

转载 作者:行者123 更新时间:2023-11-29 17:57:03 25 4
gpt4 key购买 nike

我在使用 google geocoding api 时遇到问题。例如,使用此网址 http://maps.google.com/maps/api/geocode/json?latlng=47.3195254,5.0430687&sensor=true ,我想用 jackson 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

最佳答案

我是 Jackson 的粉丝,我可以说你让我很好奇。我使用了您公开的 URL 并使其像下面一样工作。 使用当前 API 版本:2.2.3

模型类:

地理编码器结果

public class GeocoderResult {

@JsonProperty("results")
private ArrayList<GeocoderGoog> geocoder;

@JsonProperty("status")
private String status;

public ArrayList<GeocoderGoog> getGeocoder() {
return geocoder;
}

public String getStatus() {
return status;
}
}

GeocoderGoog

public class GeocoderGoog {

@JsonProperty("address_components")
private ArrayList<AddressComponent> addressComponents;

@JsonProperty("formatted_address")
private String formattedAddress;

private ArrayList<String> types;

private Geometry geometry;

public ArrayList<AddressComponent> getAddressComponents() {
return addressComponents;
}

public String getFormattedAddress() {
return formattedAddress;
}

public ArrayList<String> getTypes() {
return types;
}

public Geometry getGeometry() {
return geometry;
}

}

地址组件

public class AddressComponent {
@JsonProperty("long_name")
private String longName;

@JsonProperty("short_name")
private String shortName;

private ArrayList<String> types;

public String getLongName() {
return longName;
}

public String getShortName() {
return shortName;
}

public ArrayList<String> getTypes() {
return types;
}

}

在其他类中使用的坐标

public class Coordinates {
private double lat;
private double lng;

public double getLat() {
return lat;
}

public void setLat(double lat) {
this.lat = lat;
}

public double getLng() {
return lng;
}

public void setLng(double lng) {
this.lng = lng;
}

}

和一个带有ViewportGeometry:

public class Geometry {
private Coordinates location;

@JsonProperty("location_type")
private String locationType;

private ViewPort viewport;

public Coordinates getLocation() {
return location;
}

public String getLocationType() {
return locationType;
}

public ViewPort getViewport() {
return viewport;
}

public static class ViewPort {
private Coordinates northeast;
private Coordinates southwest;

public Coordinates getNortheast() {
return northeast;
}

public Coordinates getSouthwest() {
return southwest;
}

}
}

总结一下——对我来说这从第一次尝试开始就成功了:

protected void performJackson() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
String baseUrl = "http://maps.google.com/maps/api/geocode/json?latlng=47.3195254,5.0430687&sensor=true";
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("SJackson", "Status : " + statusCode);
if (statusCode == HttpURLConnection.HTTP_OK) { // 200
InputStream is = new BufferedInputStream(connection.getInputStream());
GeocoderResult result = mapper.readValue(is, GeocoderResult.class);
Log.d("SJackson", "Done: " + (result != null));
}
} catch (Exception ex) {
Log.e("SJackson", null, ex);
}

return null;
}
}.execute();
}

关于android - jackson 2 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18630877/

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