如何通过 google-api-client 从数组开始反序列化 json
很像如何反序列化以Jackson 中的数组? 我有一个 JSON REST 回复,看起来像
[
{
something
},
{
something
},
...
]
问题是该数组是顶级的。我尝试致电 com.google.api.client.http.json.JsonHttpParser
如下:
Something[] result = jsonParser.parse(response, Something[].class); //array cannot be instantiated: java.lang.IllegalArgumentException: unable to create new instance of class [L...; because it is an array
List<Something> result = jsonParser.parse(response, List.class); //interface cannot be instantiated: java.lang.IllegalArgumentException: unable to create new instance of class java.util.List because it is an interface and because it has no accessible default constructor
ArrayList<Something> result = jsonParser.parse(response, ArrayList.class); //throws IllegalArgumentException: START_ARRAY
其中 Something
是我的模型类,应该允许反序列化元素。这些替代方案都不起作用。最后一个的堆栈跟踪是:
01-21 18:44:15.649: E/AndroidRuntime(3117): Caused by: java.lang.IllegalArgumentException: START_ARRAY
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.common.base.Preconditions.checkArgument(Preconditions.java:88)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.startParsingObject(JsonParser.java:161)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.parse(JsonParser.java:233)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.parse(JsonParser.java:224)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:180)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:120)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.http.json.JsonHttpParser.parse(JsonHttpParser.java:62)
我应该怎么做?
Much like How to deserialize JSON file starting with an array in Jackson? I have a JSON REST reply that looks like
[
{
something
},
{
something
},
...
]
Problem is that the array is toplevel. I tried calling com.google.api.client.http.json.JsonHttpParser
as follows:
Something[] result = jsonParser.parse(response, Something[].class); //array cannot be instantiated: java.lang.IllegalArgumentException: unable to create new instance of class [L...; because it is an array
List<Something> result = jsonParser.parse(response, List.class); //interface cannot be instantiated: java.lang.IllegalArgumentException: unable to create new instance of class java.util.List because it is an interface and because it has no accessible default constructor
ArrayList<Something> result = jsonParser.parse(response, ArrayList.class); //throws IllegalArgumentException: START_ARRAY
where Something
is my model class that should allow deserialization of an element. None of these alternatives work. The stacktrace on the last one is:
01-21 18:44:15.649: E/AndroidRuntime(3117): Caused by: java.lang.IllegalArgumentException: START_ARRAY
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.common.base.Preconditions.checkArgument(Preconditions.java:88)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.startParsingObject(JsonParser.java:161)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.parse(JsonParser.java:233)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.parse(JsonParser.java:224)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:180)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:120)
01-21 18:44:15.649: E/AndroidRuntime(3117): at com.google.api.client.http.json.JsonHttpParser.parse(JsonHttpParser.java:62)
How should I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
google-api-client 1.4.1 类似乎仅依赖于创建新实例,并且在此区域中仅使用 START_OBJECT,而不使用 START_ARRAY,这意味着它们不支持顶级数组。
我无法解决这个问题,我通过将所有内容更新到 google-api-client 1.6.0 来解决它,这是一个相当大的更新和重构工作。
The google-api-client 1.4.1 classes just seem to rely on creating new instances and only working with START_OBJECT, not START_ARRAY, in this area, meaning they don't support toplevel arrays.
I couldn't workaround this and I solved it by updating everything to google-api-client 1.6.0, which is quite a large update and refactor effort.