Error:com.google.gson.internal.LinkedTreeMap cannot be cast to ***

本文解决在数据转换过程中遇到的错误,如从LinkedTreeMap或LinkedHashMap转换到Map接口的问题,提供有效的代码示例。

数据转换错误,类似的错误还有LinkedHashMap,这个时候,我们应该将子类向父类或接口转换,因为LinkedTreeMap和LinkedHashMap都实现了Map。转换代码:

Map<String, Object> infoMap = (Map<String, Object>) linkTreeMap;

Map<String, Object> infoMap = (Map<String, Object>) linkHashMap;

亲测有效!!!

2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E [app] [clientID] [172.16.1.15] [DefaultDispatcher-worker-7:(ID=393)] [JafViewModel:showError:242] [jp.or.jaf.syg.feature.jafrsho07.jafrsho07030.JAFRSHO07030ViewModel.jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo:-1] – 処理エラー | duration -1ms 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E java.lang.ClassCastException: jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository$getTShireiData$2.invokeSuspend(JAFRSHO07030Repository.kt:81) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository$getTShireiData$2.invoke(Unknown Source:8) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository$getTShireiData$2.invoke(Unknown Source:4) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:78) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:167) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.BuildersKt.withContext(Unknown Source:1) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository.getTShireiData(JAFRSHO07030Repository.kt:80) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at java.lang.reflect.Method.invoke(Native Method) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.core.common.aspect.CombinedInvocationHandler.handleSuspendFunction(CombinedInvocationHandler.kt:176) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.core.common.aspect.CombinedInvocationHandler.invoke(CombinedInvocationHandler.kt:68) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at java.lang.reflect.Proxy.invoke(Proxy.java:1006) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at $Proxy133.getTShireiData(Unknown Source) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.domain.usecase.jafrsho07.jafrsho07030.impl.JAFRSHO07030E001UseCase2.invoke-gIAlu-s(JAFRSHO07030E001UseCase2.kt:40) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.domain.usecase.jafrsho07.jafrsho07030.impl.JAFRSHO07030E001UseCase2.invoke-gIAlu-s(JAFRSHO07030E001UseCase2.kt:34) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at java.lang.reflect.Method.invoke(Native Method) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.core.common.aspect.CombinedInvocationHandler.handleSuspendFunction(CombinedInvocationHandler.kt:176) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.core.common.aspect.CombinedInvocationHandler.invoke(CombinedInvocationHandler.kt:68) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at java.lang.reflect.Proxy.invoke(Proxy.java:1006) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at $Proxy135.invoke-gIAlu-s(Unknown Source) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.feature.jafrsho07.jafrsho07030.JAFRSHO07030ViewModel.generateNoticeMessage(JAFRSHO07030ViewModel.kt:170) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.feature.jafrsho07.jafrsho07030.JAFRSHO07030ViewModel.onPopupDisplayTest(JAFRSHO07030ViewModel.kt:207) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.feature.jafrsho07.jafrsho07030.JAFRSHO07030ViewModel$onEvent$1.invokeSuspend(JAFRSHO07030ViewModel.kt:75) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.feature.jafrsho07.jafrsho07030.JAFRSHO07030ViewModel$onEvent$1.invoke(Unknown Source:8) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.feature.jafrsho07.jafrsho07030.JAFRSHO07030ViewModel$onEvent$1.invoke(Unknown Source:2) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.core.common.base.JafViewModel$launchWithLoading$1.invokeSuspend(JafViewModel.kt:328) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.core.common.base.JafViewModel$launchWithLoading$1.invoke(Unknown Source:8) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.core.common.base.JafViewModel$launchWithLoading$1.invoke(Unknown Source:2) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at jp.or.jaf.syg.core.common.ext.JafCoroutinesKt$runOnIO$2.invokeSuspend(JafCoroutines.kt:133) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684) 2025-09-23 20:04:11.040 8135-8181 JafCommonLog jp.or.jaf.syg E 2025-09-23 20:04:11.082 8135-8146 jp.or.jaf.syg jp.or.jaf.syg I Background concurrent copying GC freed 156347(11MB) AllocSpace objects, 57(1148KB) LOS objects, 49% free, 11MB/22MB, paused 23us total 160.816ms 2025-09-23 20:04:11.086 8135-8135 メッセージダイアログ jp.or.jaf.syg I size:3 2025-09-23 20:04:11.086 8135-8135 メッセージダイアログ jp.or.jaf.syg I メッセージが見つかりません 2025-09-23 20:04:11.086 8135-8135 メッセージダイアログ jp.or.jaf.syg I getResult: 2025-09-23 20:04:11.087 8135-8135 JafCommonLog jp.or.jaf.syg I [app] [clientID] [172.16.1.15] [main:(ID=2)] [Jafrsho99020MessagePopupKt:Jafrsho99020MessagePopup:invoke:170] 2025-09-23 20:04:11.090 8135-8181 JafCommonLog jp.or.jaf.syg D [app] [clientID] [172.16.1.15] [DefaultDispatcher-worker-7:(ID=393)] [JafLoggingAspect:beforeMethod:55] [jp.or.jaf.syg.domain.usecase.jafrsho07.jafrsho07030.impl.JAFRSHO07030E001UseCase2.invoke-gIAlu-s(ShireiCancelNoticeMo, Continuation at jp.o):-1] – 処理開始 2025-09-23 20:04:11.091 8135-8181 JafCommonLog jp.or.jaf.syg D [app] [clientID] [172.16.1.15] [DefaultDispatcher-worker-7:(ID=393)] [JafLoggingAspect:beforeMethod:55] [jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository.getTShireiData(100000001, Continuation at jp.o):-1] – 処理開始 2025-09-23 20:04:11.093 8135-8181 JafCommonLog jp.or.jaf.syg D [app] [clientID] [172.16.1.15] [DefaultDispatcher-worker-7:(ID=393)] [JafLoggingAspect:beforeMethod:55] [jp.or.jaf.syg.data.local.dao.transaction.TShireiDataDao_Impl.getTShireiData3(100000001, Continuation at jp.o):-1] – 処理開始 2025-09-23 20:04:11.096 8135-8176 JafCommonLog jp.or.jaf.syg D [app] [clientID] [172.16.1.15] [arch_disk_io_2:(ID=388)] [JafLoggingAspect:afterMethod:82] [jp.or.jaf.syg.data.local.dao.transaction.TShireiDataDao_Impl.getTShireiData3:-1] – 処理終了 [=> Result: TShireiData(base=TShireiDataBa] | duration 3ms 2025-09-23 20:04:11.098 8135-8176 JafCommonLog jp.or.jaf.syg E [app] [clientID] [172.16.1.15] [arch_disk_io_2:(ID=388)] [JafLoggingAspect:onError:98] [jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository.getTShireiData:-1] – 処理エラー | duration 6ms jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo java.lang.ClassCastException: jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo at jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository$getTShireiData$2.invokeSuspend(JAFRSHO07030Repository.kt:81) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at jp.or.jaf.syg.core.common.aspect.CombinedInvocationHandler$CombinedLoggingContinuation.resumeWith(CombinedInvocationHandler.kt:222) at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:179) at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:168) at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497) at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:368) at androidx.room.CoroutinesRoom$Companion$execute$4$job$1.invokeSuspend(CoroutinesRoom.kt:88) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) 2025-09-23 20:04:11.099 8135-8176 JafCommonLog jp.or.jaf.syg E [app] [clientID] [172.16.1.15] [arch_disk_io_2:(ID=388)] [JafLoggingAspect:onError:98] [jp.or.jaf.syg.domain.usecase.jafrsho07.jafrsho07030.impl.JAFRSHO07030E001UseCase2.invoke-gIAlu-s:-1] – 処理エラー | duration 11ms jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo java.lang.ClassCastException: jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo at jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository$getTShireiData$2.invokeSuspend(JAFRSHO07030Repository.kt:81) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at jp.or.jaf.syg.core.common.aspect.CombinedInvocationHandler$CombinedLoggingContinuation.resumeWith(CombinedInvocationHandler.kt:222) at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:179) at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:168) at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497) at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:368) at androidx.room.CoroutinesRoom$Companion$execute$4$job$1.invokeSuspend(CoroutinesRoom.kt:88) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) 2025-09-23 20:04:11.101 8135-8176 JafCommonLog jp.or.jaf.syg E [app] [clientID] [172.16.1.15] [arch_disk_io_2:(ID=388)] [JafViewModel:showError:242] [jp.or.jaf.syg.feature.jafrsho07.jafrsho07030.JAFRSHO07030ViewModel.jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo:-1] – 処理エラー | duration -1ms jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo java.lang.ClassCastException: jp.or.jaf.syg.domain.model.jafrsho06.jafrsho06060.ShireiDataInfo cannot be cast to jp.or.jaf.syg.domain.model.jafrsho07.jafrsho07030.TShireiDataInfo at jp.or.jaf.syg.data.repository.jafrsho07.JAFRSHO07030Repository$getTShireiData$2.invokeSuspend(JAFRSHO07030Repository.kt:81) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at jp.or.jaf.syg.core.common.aspect.CombinedInvocationHandler$CombinedLoggingContinuation.resumeWith(CombinedInvocationHandler.kt:222) at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:179) at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:168) at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497) at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:368) at androidx.room.CoroutinesRoom$Companion$execute$4$job$1.invokeSuspend(CoroutinesRoom.kt:88) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) 2025-09-23 20:04:11.142 8135-8170 OpenGLRenderer jp.or.jaf.syg D endAllActiveAnimators on 0x76388889b500 (UnprojectedRipple) with handle 0x763868036120 2025-09-23 20:04:11.190 1564-1578 memtrack system_server E Couldn't load memtrack module 2025-09-23 20:04:11.190 1564-1578 android.os.Debug system_server W failed to get memory consumption info: -1 2025-09-23 20:04:11.195 1434-1649 SurfaceFlinger surfaceflinger W Attempting to set client state on removed layer: jp.or.jaf.syg/jp.or.jaf.syg.SygMainActivity#1 2025-09-23 20:04:11.195 1434-1649 SurfaceFlinger surfaceflinger W Attempting to destroy on removed layer: jp.or.jaf.syg/jp.or.jaf.syg.SygMainActivity#1 2025-09-23 20:04:11.245 1434-2114 SurfaceFlinger surfaceflinger W Attempting to set client state on removed layer: Dim Layer for - Task=61#0 2025-09-23 20:04:11.245 1434-2114 SurfaceFlinger surfaceflinger W Attempting to destroy on removed layer: Dim Layer for - Task=61#0
09-24
出现 `java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.qish.tenmentapp.entity.House` 异常,是因为 `result.getData()` 返回的是 `LinkedTreeMap` 类型的数据,不能直接将其强制转换为 `List<House>` 类型。可以通过将数据先转换为 JSON 字符串,再将 JSON 字符串转换为对应的 `House` 对象列表来解决。 以下是解决该问题的示例代码: ```java import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import java.util.ArrayList; import java.util.List; // 假设 House 类的定义如下 class House { // House 类的属性和方法 } // 假设 Result 类的定义如下 class Result { private Object data; public Object getData() { return data; } public void setData(Object data) { this.data = data; } } public class Main { public static <T> List<T> getObjectList(String jsonString, Class<T> cls) { List<T> list = new ArrayList<>(); try { Gson gson = new Gson(); JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray(); for (JsonElement jsonElement : arry) { list.add(gson.fromJson(jsonElement, cls)); } } catch (Exception e) { e.printStackTrace(); } return list; } public static void main(String[] args) { Result result = new Result(); // 假设 result.getData() 返回的数据 // 这里省略了具体的赋值操作 // 将 data 转换为 JSON 字符串 Gson gson = new Gson(); String jsonString = gson.toJson(result.getData()); // 将 JSON 字符串转换为 List<House> List<House> records = getObjectList(jsonString, House.class); } } ``` 在上述代码中,首先定义了 `House` 类和 `Result` 类,然后实现了 `getObjectList` 方法,该方法用于将 JSON 字符串转换为指定类型的对象列表。在 `main` 方法中,将 `result.getData()` 转换为 JSON 字符串,再调用 `getObjectList` 方法将 JSON 字符串转换为 `List<House>`。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data-Mining

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值