问题:使用 Jersey 序列化,使用 gson 反序列化 JSON
我一直在使用 Jersey 进行 REST API 并返回 JSON。在客户端,我使用 google-gson。反序列化 JSON 时,出现以下错误。
com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object {} given the type interface java.util.Map
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonDeserializationCo
Jersey 序列化为 JSON 的对象具有非通用(未指定类型)映射和列表
public class Dealer implements Serializable
{
private String serviceURL;
private Map hoursService;
List dealerAttributes;
}
这是 Jersey 生成的 JSON
{"serviceURL":www.google.com,"hoursService":{"3":{"dayOfWeek":3,"closeTime":"6:30 PM","openTime":"7:30 AM"},"2":{"dayOfWeek":2,"closeTime":"6:30 PM","openTime":"7:30 AM"},"1":{"dayOfWeek":1,"closeTime":"6:30 PM","openTime":"7:30 AM"}},"dealerAttributes":[{"language":"English","dealerAttributeName":"Spanish Speaking","updateDate":1174971061000},{"language":"English","updateDate":1103003316000}]}
可能是什么原因。请帮我解决这个问题。还有其他比 gson 更好的实用程序吗?请建议。
谢谢。
I have been using Jersey for REST API and returning JSON. On client side I am using google-gson. While deserializing JSON, I am getting following error.
com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object {} given the type interface java.util.Map
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonDeserializationCo
The Object which is being serialized to JSON by Jersey is having non-generic(no type specified) Map and List
public class Dealer implements Serializable
{
private String serviceURL;
private Map hoursService;
List dealerAttributes;
}
Here is the JSON generated by Jersey
{"serviceURL":www.google.com,"hoursService":{"3":{"dayOfWeek":3,"closeTime":"6:30 PM","openTime":"7:30 AM"},"2":{"dayOfWeek":2,"closeTime":"6:30 PM","openTime":"7:30 AM"},"1":{"dayOfWeek":1,"closeTime":"6:30 PM","openTime":"7:30 AM"}},"dealerAttributes":[{"language":"English","dealerAttributeName":"Spanish Speaking","updateDate":1174971061000},{"language":"English","updateDate":1103003316000}]}
What could be the reason. Please help me to resolve this issue. Is there any other utility which is better than gson? Please suggest.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
示例 JSON 无效。 http://jsonlint.com
www.google.com 必须用引号引起来。那么它就有效了。
纠正 JSON 后,以下反序列化示例输出
This does use generic types,我理解你说你没有使用它来序列化,但是你在反序列化时是否遇到了相同的限制?如果是这样,那么您将必须实施更多“手动”反序列化解析,因为正如文档所说,"[w]反序列化时,集合必须是特定的泛型类型"。
The example JSON is invalid. http://jsonlint.com
www.google.com must be enclosed with quotes. Then it is valid.
With the JSON corrected, the following deserialization example outputs
This does use generic types, which I understand you said you weren't using to serialize, but are you stuck with the same limitation when deserialization? If so, then you'll have to implement some more "manual" deserialization parsing, because, as the docs say, "[w]hile deserializing, Collection must be of a specific generic type".