Gson(Json)解析异常

时间:2011-09-24 13:27:49

标签: java json gson

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

以下是例外情况:

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
}

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

此致

1 个答案:

答案 0 :(得分:1)

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

错误 对我有点好奇,因为Gson通常会从无法映射的属性中打印出JSON。在您的情况下,CGLIB$BOUNDboolean,但在这种情况下Gson表现得很好,为您提供String"true"。如果您提供要反序列化的POJO,我们可以更准确地识别您的问题。