gpt4 book ai didi

java - Gson(Json)解析异常

转载 作者:行者123 更新时间:2023-12-01 15:43:48 25 4
gpt4 key购买 nike

我在使用 Gson 解析 JSON 时遇到异常。

以下是异常(exception)情况:

com.google.gson.JsonParseException: The JsonDeserializer StringTypeAdapter failed to deserialize json object {"CGLIB$BOUND":true,"CGLIB$CONSTRUCTED":true,"CGLIB$CALLBACK_0":{"interfaces":[{}],"constructed":true,"persistentClass":{},"getIdentifierMethod":{"clazz":{},"slot":0,"name":"getmId","returnType":{},"parameterTypes":[],"exceptionTypes":[],"modifiers":1,"annotations":[0,3,0,67,0,0,0,68,0,0,0,69,0,1,0,70,115,0,71],"root":{"clazz":{},"slot":0,"name":"getmId","returnType":{},"parameterTypes":[],"exceptionTypes":[],"modifiers":1,"annotations":[0,3,0,67,0,0,0,68,0,0,0,69,0,1,0,70,115,0,71],"override":false},"override":false},"setIdentifierMethod":{"clazz":{},"slot":1,"name":"setmId","returnType":{},"parameterTypes":[{}],"exceptionTypes":[],"modifiers":1,"root":{"clazz":{},"slot":1,"name":"setmId","returnType":{},"parameterTypes":[{}],"exceptionTypes":[],"modifiers":1,"override":false},"override":false},"overridesEquals":false,"initialized":false,"entityName":"com.domain.Hotel","id":1,"unwrap":false},"mId":0,"mHotelLatitude":0.0,"mHotelLongitude":0.0,"mHotelRating":0.0,"mHotelAvgPrice":0.0} given the type class java.lang.String

JSON:

{
"CGLIB$BOUND": true,
"CGLIB$CONSTRUCTED": true,
"CGLIB$CALLBACK_0": {
"interfaces": [
{}
],
"constructed": true,
"persistentClass": {},
"getIdentifierMethod": {
"clazz": {},
"slot": 0,
"name": "getmId",
"returnType": {},
"parameterTypes": [],
"exceptionTypes": [],
"modifiers": 1,
"annotations": [
0,
3,
0,
67,
0,
0,
0,
68,
0,
0,
0,
69,
0,
1,
0,
70,
115,
0,
71
],
"root": {
"clazz": {},
"slot": 0,
"name": "getmId",
"returnType": {},
"parameterTypes": [],
"exceptionTypes": [],
"modifiers": 1,
"annotations": [
0,
3,
0,
67,
0,
0,
0,
68,
0,
0,
0,
69,
0,
1,
0,
70,
115,
0,
71
],
"override": false
},
"override": false
},
"setIdentifierMethod": {
"clazz": {},
"slot": 1,
"name": "setmId",
"returnType": {},
"parameterTypes": [
{}
],
"exceptionTypes": [],
"modifiers": 1,
"root": {
"clazz": {},
"slot": 1,
"name": "setmId",
"returnType": {},
"parameterTypes": [
{}
],
"exceptionTypes": [],
"modifiers": 1,
"override": false
},
"override": false
},
"overridesEquals": false,
"initialized": false,
"entityName": "com.domain.Hotel",
"id": 1,
"unwrap": false
},
"mId": 0,
"mHotelLatitude": 0,
"mHotelLongitude": 0,
"mHotelRating": 0,
"mHotelAvgPrice": 0
}

有人知道为什么会出现这种异常吗?

问候

最佳答案

我可以在 Gson 中解析这个 JSON。当您错误地将 JSON 属性类型映射到 POJO 中的 Java 成员类型(例如,JSON 中的数组类型在 POJO 中声明为 String 类型)时,会生成上述错误。

这个错误让我有点好奇,因为 Gson 通常会从无法映射的属性中打印出 JSON。在您的情况下,这将是 CGLIB$BOUND ,它是一个 boolean,但 Gson 在这种情况下表现良好,为您提供一个 String“真”。如果您提供尝试反序列化的 POJO,我们可以更准确地识别您的问题。

关于java - Gson(Json)解析异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7539327/

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