gpt4 book ai didi

java - 使用 json 反序列化 java 泛型集合

转载 作者:行者123 更新时间:2023-11-30 05:04:56 27 4
gpt4 key购买 nike

我正在尝试序列化和反序列化来自 hibernate 的答案。
几乎成功了。
首先,我使用

从数据库获取数据(对象 DealBean)
Collections.checkedList(session.createCriteria(DealBean.class).add(Restrictions.eq("account", account)).list(), DealBean.class);

然后我将其序列化(使用 gson)并将结果发送到客户端。

在客户端我尝试使用反序列化它

Type type = new TypeToken<Collection<DealBean>>(){}.getType();
List<DealBean> dealsForAccount = (List<DealBean>)Json.from(SomeAnswerString);

现在 - 我得到 JsonParseException。我认为从hibernate返回的列表是另一种类型java.util.Collections$CheckedRandomAccessList我得到以下异常(如下)。1.有什么建议吗?2.我选择正确吗?我可以在服务器上转换结果吗?

JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@a17378 failed to deserialized json object {"type":"java.util.Collections$CheckedRandomAccessList","content":"[{\"ID\":5,\"tradable\":{\"id\":5,\"tradableName\":\"Tradable1\",\"moneyMultiplier\":1},\"Action\":\"buy\",\"amount\":501.0,\"price\":50.3,\"portfolio\":{\"ID\":2,\"portfolioName\":\"Portfolio2\"},\"account\":{\"ID\":1,\"accountName\":\"Account1\",\"bank\":{\"ID\":1,\"BankName\":\"Bank1\"}},\"date\":\"12:17:36 02/01/1970\",\"activitySum\":0.0,\"aggregated\":false},{\"ID\":6,\"tradable\":{\"id\":6,\"tradableName\":\"Tradable1\",\"moneyMultiplier\":1},\"Action\":\"sell\",\"amount\":1001.0,\"price\":50.4,\"portfolio\":{\"ID\":2,\"portfolioName\":\"Portfolio2\"},\"account\":{\"ID\":2,\"accountName\":\"Account2\",\"bank\":{\"ID\":2,\"BankName\":\"Bank2\"}},\"date\":\"12:17:36 02/01/1970\",\"activitySum\":0.0,\"aggregated\":false},{\"ID\":7,\"tradable\":{\"id\":7,\"tradableName\":\"Tradable2\",\"moneyMultiplier\":1},\"Action\":\"buy\",\"amount\":502.0,\"price\":50.5,\"portfolio\":{\"ID\":2,\"portfolioName\":\"Portfolio2\"},\"account\":{\"ID\":1,\"accountName\":\"Account1\",\"bank\":{\"ID\":1,\"BankName\":\"Bank1\"}},\"date\":\"12:17:36 02/01/1970\",\"activitySum\":0.0,\"aggregated\":false},{\"ID\":8,\"tradable\":{\"id\":8,\"tradableName\":\"Tradable2\",\"moneyMultiplier\":1},\"Action\":\"sell\",\"amount\":1002.0,\"price\":50.6,\"portfolio\":{\"ID\":2,\"portfolioName\":\"Portfolio2\"},\"account\":{\"ID\":2,\"accountName\":\"Account2\",\"bank\":{\"ID\":2,\"BankName\":\"Bank2\"}},\"date\":\"12:17:36 02/01/1970\",\"activitySum\":0.0,\"aggregated\":false}]"} given the type java.util.Collection at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63) at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88) at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76) at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:98) at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73) at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51) at com.google.gson.Gson.fromJson(Gson.java:495) at com.google.gson.Gson.fromJson(Gson.java:444) at com.google.gson.Gson.fromJson(Gson.java:396) at positions.server.services.PositionServerServicesInternalWrapperTest.testGetPositionsForPortfolio(PositionServerServicesInternalWrapperTest.java:134) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65) Caused by: java.lang.IllegalStateException: This is not a JSON Array. at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:100) at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:468) at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:435) at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:50) ... 31 more

List<BankBean> al = new ArrayList<BankBean>();
al.add(bank1);
al.add(bank2);
String alStr = Json.toWrap(al); //

List<BankBean> reAl;

Gson gson = new Gson();
Type type = new TypeToken<Collection<BankBean>>(){}.getType();
List<String> listFromJson = gson.fromJson(alStr,type);

最佳答案

类型java.util.Collections$CheckedRandomAccessList Collections.checkedList 制作的 wrapper 另一个列表,确保添加到列表中的任何内容都与适当的类型签名匹配。除非您有某种方法指定如何创建支持列表和它应该检查的类型(又名 java.lang.Class 的实例),否则没有足够的信息来构建一个。 p>

要修复它,可能需要更改 "java.util.Collections$CheckedRandomAccessList""java.util.ArrayList"并调用Collections.checkedList使用适当的保护类型进行反序列化的结果。

为了防止问题再次出现,如果您的列表不是 instanceof ArrayList<?>,则可以在序列化之前执行以下操作。 :

List<Object> mySerializableList = new ArrayList<Object>(myList);

关于java - 使用 json 反序列化 java 泛型集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5421777/

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