2011-09-24 169 views
0

在使用Gson解析JSON時出現異常。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 
} 

有誰知道爲什麼這個異常會來的想法?

問候

+0

什麼是你解析的JSON,以及你想要反序列化到什麼類型?你用什麼代碼來做到這一點? –

回答

1

我能得到這個JSON在GSON解析。如果在POJO中錯誤地將JSON屬性類型映射爲Java成員類型(例如,JSON中的數組類型在POJO中聲明爲String類型),則會生成上述錯誤。

錯誤對我有點好奇,因爲Gson通常會打印出無法映射的屬性的JSON。在你的情況下,這將是CGLIB$BOUND這是一個boolean,但Gson在這種情況下表現很好,給你一個String"true"。如果您提供您試圖反序列化的POJO,我們可以更準確地識別您的問題。

+0

我發現了這個錯誤。我已經將一個對象映射到一個字符串! – Vaibhav

+0

出於興趣,哪些屬性? – markdsievers