Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Jackson 2反序列化_Android_Jackson - Fatal编程技术网

Android Jackson 2反序列化

Android Jackson 2反序列化,android,jackson,Android,Jackson,我在使用谷歌地理编码api时遇到问题。例如,通过这个url,我想用jackson 2解析json以创建POJO 所以我的班级是 public class GeocoderResult { @JsonProperty("results") private List<GeocoderGoog> geocoder; @JsonProperty("status") private String status; public List<GeocoderGoog> getGeoc

我在使用谷歌地理编码api时遇到问题。例如,通过这个url,我想用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;
}

}
我有以下错误:

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

模型类:

GeocoderResult

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;
    }
}
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 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();
}
protectedvoid performJackson(){
新建异步任务(){
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
字符串baseUrl=”http://maps.google.com/maps/api/geocode/json?latlng=47.3195254,5.0430687&传感器=真”;
HttpURLConnection connection=(HttpURLConnection)新URL(baseUrl).openConnection();
ObjectMapper mapper=新的ObjectMapper();
//存在未知属性时禁用异常
禁用(反序列化功能。在未知属性上失败);
int statusCode=connection.getResponseCode();
Log.d(“SJackson”,“状态:+statusCode”);
如果(statusCode==HttpURLConnection.HTTP_OK){//200
InputStream is=new BufferedInputStream(connection.getInputStream());
GeocoderResult结果=mapper.readValue(即GeocoderResult.class);
d(“SJackson”,“Done:”+(result!=null));
}
}捕获(例外情况除外){
Log.e(“SJackson”,null,ex);
}
返回null;
}
}.execute();
}

为什么
坐标
类没有注释?您不需要注释类。。。您需要注释类实例我的意思是,它应该是
@JsonProperty(“lat”)private double lat
@JsonProperty(“lng”)private double lng
,不是吗?此外,我必须使用ArrayList而不是List?如果名称匹配,则不需要注释(如本例所示)。关于ArrayList,我必须说这是我从以前的JSON解析器中使用的东西。请随意使用列表,因为它更实用。它应该也能用。伙计,你面前有所有的代码。。。按CTRL+C,然后按CTRL+V!您获取的JSON无效——很可能缺少某些内容。与数据绑定无关,异常直接来自流解析。这可能是jackson版本的问题?否。请阅读异常消息。版本不兼容会导致与类加载截然不同的错误。这表明JSON输入被破坏——我无法推测这是为什么。您可以通过首先读取和缓冲内容,然后将其交给解析器来解决这个问题——也许还可以记录输入以排除故障。
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;
    }

}
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();
}