简体   繁体   English

gson \\ json反序列化错误

[英]gson\json deserialization error

I want to parse a JSON object using GSON. 我想使用GSON解析JSON对象。 Everything seems to work fine but when I'm trying to parse a more "complexed" object I'm getting a deserialization error. 一切似乎都可以正常工作,但是当我尝试解析一个更“复杂”的对象时,出现了反序列化错误。 The object I'm trying to parse contains some primitives, a stack and a vector collection. 我尝试解析的对象包含一些基元,堆栈和向量集合。

Is it possible that the stack\\vector causing this problem? 堆栈\\矢量是否可能导致此问题? can I somehow over come this? 我能以某种方式过来吗?

The Error: 错误:

Exception in thread "AWT-EventQueue-0" com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@4e76fba0 failed to deserialize json object [{"name_":"Human","points_":1,"alive_":true,"id_":0,"cssStyleClass":"pressed_p1"},{"name_":"Human","points_":0,"alive_":true,"id_":1,"cssStyleClass":"pressed_p2"}] given the type java.util.Vector<entities.Player>
    at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:64)
    at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)
    at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:117)
    at com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)
    at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:76)
    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:54)

Request Code 请求代码

Gson json = new Gson();
Multiplayer result = json.fromJson(answer.toString(), Multiplayer.class);

Code On Servlet Servlet上的代码

Gson gson = new Gson();
Multiplayer game = (Multiplayer)getServletContext().getAttribute("OnlineGame");
String responseStr = gson.toJson(game);

response.setContentType("application/json;charset=UTF-8");
out.write(responseStr);

String from toString() toString()中的字符串

{serializeNulls:false,serializers:{mapForTypeHierarchy:{Map:MapTypeAdapter,Collection:com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@596e1fb1,InetAddress:com.google.gson.DefaultTypeAdapters$DefaultInetAddressAdapter@4ce2cb55,Enum:EnumTypeAdapter},map:{Integer:IntegerTypeAdapter,URI:UriTypeAdapter,UUID:UuidTypeAdapter,BigInteger:BigIntegerTypeAdapter,URL:UrlTypeAdapter,Short:ShortTypeAdapter,Time:com.google.gson.DefaultTypeAdapters$DefaultTimeTypeAdapter@16bdb503,byte:ByteTypeAdapter,short:ShortTypeAdapter,Number:NumberTypeAdapter,double:com.google.gson.DefaultTypeAdapters$DoubleSerializer@b6e39f,GregorianCalendar:GregorianCalendarTypeAdapter,Calendar:GregorianCalendarTypeAdapter,Byte:ByteTypeAdapter,StringBuilder:StringBuilderTypeAdapter,Float:com.google.gson.DefaultTypeAdapters$FloatSerializer@6719dc16,Locale:LocaleTypeAdapter,StringBuffer:StringBufferTypeAdapter,Date:DefaultDateTypeAdapter(SimpleDateFormat),Character:CharacterTypeAdapter,float:com.google.gson.DefaultTypeAdapters$FloatSerializer@6719dc16,BigDecimal:BigDecimalTypeAdapter,Boolean:BooleanTypeAdapter,boolean:BooleanTypeAdapter,String:StringTypeAdapter,Timestamp:DefaultDateTypeAdapter(SimpleDateFormat),int:IntegerTypeAdapter,long:LongSerializer,Date:com.google.gson.DefaultTypeAdapters$DefaultJavaSqlDateTypeAdapter@52c05d3b,char:CharacterTypeAdapter,Double:com.google.gson.DefaultTypeAdapters$DoubleSerializer@b6e39f,Long:LongSerializer},deserializers:{mapForTypeHierarchy:{Map:MapTypeAdapter,Collection:com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@596e1fb1,InetAddress:com.google.gson.DefaultTypeAdapters$DefaultInetAddressAdapter@4ce2cb55,Enum:EnumTypeAdapter},map:{Integer:IntegerTypeAdapter,URI:UriTypeAdapter,UUID:UuidTypeAdapter,BigInteger:BigIntegerTypeAdapter,URL:UrlTypeAdapter,Short:ShortTypeAdapter,byte:ByteTypeAdapter,Time:com.google.gson.DefaultTypeAdapters$DefaultTimeTypeAdapter@16bdb503,short:ShortTypeAdapter,Number:NumberTypeAdapter,double:DoubleDeserializer,Byte:ByteTypeAdapter,Calendar:GregorianCalendarTypeAdapter,GregorianCalendar:GregorianCalendarTypeAdapter,StringBuilder:StringBuilderTypeAdapter,Float:FloatDeserializer,Locale:LocaleTypeAdapter,StringBuffer:StringBufferTypeAdapter,Date:DefaultDateTypeAdapter(SimpleDateFormat),Character:CharacterTypeAdapter,float:FloatDeserializer,BigDecimal:BigDecimalTypeAdapter,Boolean:BooleanTypeAdapter,boolean:BooleanTypeAdapter,String:StringTypeAdapter,Timestamp:com.google.gson.DefaultTypeAdapters$DefaultTimestampDeserializer@5328f6ee,int:IntegerTypeAdapter,long:LongDeserializer,Date:com.google.gson.DefaultTypeAdapters$DefaultJavaSqlDateTypeAdapter@52c05d3b,char:CharacterTypeAdapter,Double:DoubleDeserializer,Long:LongDeserializer},instanceCreators:{mapForTypeHierarchy:{SortedSet:DefaultConstructorCreator,Set:DefaultConstructorCreator,Queue:DefaultConstructorCreator,Collection:DefaultConstructorCreator,Map:DefaultConstructorCreator},map:{}}

I recommend you to use an array Player[] instead of the generic collection Vector<Player> . 我建议您使用数组Player[]代替通用集合Vector<Player> It's not easy to use Java generics for serializing and deserializing. 使用Java泛型进行序列化和反序列化并不容易。 If you use an array instead, you will have no problem with it. 如果改用数组,则不会有任何问题。

But it had been easier to answer if you show your code for Multiplayer and Player . 但是,如果您显示MultiplayerPlayer代码,答案就更容易了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM