gpt4 book ai didi

java - gson\json反序列化错误

转载 作者:行者123 更新时间:2023-12-01 05:39:37 26 4
gpt4 key购买 nike

我想使用 GSON 解析 JSON 对象。一切似乎都工作正常,但是当我尝试解析更“复杂”的对象时,我遇到了反序列化错误。我试图解析的对象包含一些原语、一个堆栈和一个 vector 集合。

是否有可能是 stack\vector 导致了这个问题?我能以某种方式克服这个吗?

错误:

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)

请求代码

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

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);

来自 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:{}}

最佳答案

我建议您使用数组 Player[]而不是通用集合 Vector<Player> 。使用 Java 泛型进行序列化和反序列化并不容易。如果您使用数组来代替,则不会有任何问题。

但是如果您显示 Multiplayer 的代码,回答会更容易和Player .

关于java - gson\json反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7500461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com