GSON和Flickr API问题:反序列化json时出现JsonParseException

时间:2010-06-24 01:41:30

标签: java android eclipse json flickr

我对java和android开发比较陌生,如果这是一个愚蠢的问题,那就很抱歉。我正在构建简单的应用程序,只是为了学习安卓绳索,它将显示上传到flickr的最新照片列表。该应用程序已构建,但当它试图反序列化我从flickr返回的json时,它正在崩溃。这是我的错误:

06-23 21:11:25.560: WARN/System.err(298): com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@43e28168 failed to deserialized json object {"page":1,"pages":100,"perpage":10,"total":1000,"photo":[{"id":"4728507833","owner":"59492791@N00","secret":"496b96968d","server":"1407","farm":2,"title":"DSCN2733","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1407/4728507833_496b96968d_s.jpg","height_sq":75,"width_sq":75},{"id":"4728507883","owner":"30728507@N07","secret":"ab80a5bdf4","server":"1194","farm":2,"title":"Sultan","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1194/4728507883_ab80a5bdf4_s.jpg","height_sq":75,"width_sq":75},{"id":"4728507955","owner":"30311414@N00","secret":"7d6ea1cd08","server":"1377","farm":2,"title":"DSC04455","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1377/4728507955_7d6ea1cd08_s.jpg","height_sq":75,"width_sq":75},{"id":"4728507979","owner":"24737003@N04","secret":"c76cfe4afb","server":"1162","farm":2,"title":"IMGP1429","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1162/4728507979_c76cfe4afb_s.jpg","height_sq":75,"width_sq":75},{"id":"4728508009","owner":"62084706@N00","secret":"76b858efe7","server":"1366","farm":2,"title":"DSC_2459.JPG","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1366/4728508009_76b858efe7_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153856","owner":"50879555@N02","secret":"843d4f56ec","server":"1129","farm":2,"title":"IMG_1585","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1129/4729153856_843d4f56ec_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153942","owner":"43941986@N08","secret":"f7ea2d3037","server":"1257","farm":2,"title":"IMG_1126","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1257/4729153942_f7ea2d3037_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153952","owner":"50634775@N07","secret":"af6d937dcd","server":"1129","farm":2,"title":"069_69","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1129/4729153952_af6d937dcd_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153962","owner":"34771042@N07","secret":"71f6c321eb","server":"1014","farm":2,"title":"Snapshot_010","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1014/4729153962_71f6c321eb_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153964","owner":"14318596@N08","secret":"6ab627b565","server":"1364","farm":2,"title":"DSC_0679","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1364/4729153964_6ab627b565_s.jpg","height_sq":75,"width_sq":75}]} given the type com.google.gson.ParameterizedTypeImpl@3dd03b7
06-23 21:11:25.580: WARN/System.err(298):     at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
06-23 21:11:25.580: WARN/System.err(298):     at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
06-23 21:11:25.600: WARN/System.err(298):     at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:117)
06-23 21:11:25.600: WARN/System.err(298):     at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:150)
06-23 21:11:25.610: WARN/System.err(298):     at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
...
06-23 21:11:25.851: WARN/System.err(298): Caused by: java.lang.IllegalStateException: This is not a JSON Array.
06-23 21:11:25.901: WARN/System.err(298):     at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:100)
06-23 21:11:25.911: WARN/System.err(298):     at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:468)
06-23 21:11:25.921: WARN/System.err(298):     at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:435)
06-23 21:11:25.921: WARN/System.err(298):     at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:50)
06-23 21:11:25.931: WARN/System.err(298):     ... 26 more

从,错误我假设它有一些GSON不喜欢正在返回的json,但我不知道java如何在整个操作期间转换对象和其他类型的变量。

我不确定你是否需要这个,但这里有一些来自项目的代码:

String response = client.getResponse();
response = response.replace("jsonFlickrApi(", "");
response = response.substring(0,response.length()-2);
Gson gson = new Gson();

FlickrPhotos flickrphotos = new FlickrPhotos();
try {
    flickrphotos = gson.fromJson(response, FlickrPhotos.class);
} catch(Exception e) {
    e.printStackTrace();
}

这是FlickrPhotos:

public class FlickrPhotos {
    private String page, pages, perpage, total;
    private List<FlickrPhoto> photos;

    public List<FlickrPhoto> getPhotos() {
        return photos;
    }
    // More getters
}

这是FlickrPhoto:

public class FlickrPhoto {
    private String id, owner, secret, server, title, width_sq, url_sq;
    // getters
}

有什么想法吗?非常感谢你的时间和可能的耐心...(如果我正在努力吧!)

1 个答案:

答案 0 :(得分:2)

仅仅通过观察就可以看出一件事photophotos的差异:

... "total":1000,"photo":[{ ...

VS

private List<FlickrPhoto> photos;

public List<FlickrPhoto> getPhotos() {
    return photos;
}

如果您还没有,可以尝试将List重命名为photo而不是

此外,许多属性(pagepagesperpagewidth_sq等)在JSON中不是String。我认为有一个自动类型转换器处理它,但你也可以将它们声明为int