• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Gson(Json)解析异常

gson 14次浏览

使用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
}

有谁知道为什么会出现这种例外?

问候

解决方案如下:

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

这个错误令我有些好奇,因为Gson通常会从无法映射的属性中打印出JSON。在您的情况下,这将是CGLIB$BOUND,即boolean,但Gson在这种情况下表现良好,为您提供了String"true"。如果您提供要反序列化的POJO,我们可以更准确地确定您的问题。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)