如何通过 google-api-client 从数组开始反序列化 json

发布于 2024-12-28 09:33:17 字数 2285 浏览 0 评论 0原文

很像如何反序列化以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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开柳相依 2025-01-04 09:33:17

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文