GSON 和 Flickr API 问题:反序列化 json 时出现 JsonParseException
我对 java 和 android 开发比较陌生,如果这是一个愚蠢的问题,我很抱歉。我正在构建简单的应用程序,只是为了学习 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
From,我假设它有一些 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
}
有什么想法吗?非常感谢您的时间和耐心......(如果我不介意的话!)
I'm relatively new to java and android development so sorry if this is a silly question. I'm building simple app, simply to learn the android ropes, that will display a list of the most recent photos uploaded to flickr. The app is constructed but it's crapping out when it tries to deserialize the json that I get back from flickr. Here is my error:
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
From, the error I'm assuming it has something GSON doesn't like about the json that is being returned but I don't know enough about how java converts objects and other types of variables during the whole operation.
I'm not sure if you will need this but here is some of the code from the project:
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();
}
Here is FlickrPhotos:
public class FlickrPhotos {
private String page, pages, perpage, total;
private List<FlickrPhoto> photos;
public List<FlickrPhoto> getPhotos() {
return photos;
}
// More getters
}
Here is FlickrPhoto:
public class FlickrPhoto {
private String id, owner, secret, server, title, width_sq, url_sq;
// getters
}
Any ideas? Thanks so much for your time and probably patience...(If I'm noobing it up!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅通过查看就让我印象深刻的一件事是
photo
与photos
之间的差异:vs
如果您还没有这样做,也许可以尝试重命名您的
List
改为仅照片
?此外,许多属性(
page
、pages
、perpage
、width_sq
等)不是JSON 中的字符串。我认为有一个自动类型转换器可以处理这个问题,但您也可以将它们声明为 int 。
One thing that stood out to me by just looking was this
photo
vsphotos
discrepancy:vs
If you haven't already, maybe try renaming your
List
to justphoto
instead?Also, many of the properties (
page
,pages
,perpage
,width_sq
etc) aren'tString
s in the JSON. I would think there's an automatic type converter that deals with that, but you might as well declare those asint
s.