JsonParseException:JsonDeserializer com.google.gson.DefaultTypeAdapters在运行google places api时

时间:2014-12-05 05:53:01

标签: java json deserialization json-deserialization parameterized-types

我正在运行google places api我收到的回复是json格式我还在json viewer中查看过,

{
   "html_attributions" : [],
   "next_page_token" : "CvQB7AAAAKcVCYUk92u8YbCxucwIYZZoVsNOAX1utAinxpRnjirhCWRGYI1ozjaw-9JauOVcr8iOqsvGVR3n183oJ-saMliylmFcPdJ8HwhIpxwWr8EsXDuTk4SCIYfSfcpSflBaWMqorPnNHAVMz9HS3r4q8Hq-UUobHHfD8HZ4auX9eOzVHETQezM0eT8164h1qsRChdvzEQdsxu1nze6c3GI6xO_PByuSeI8tksFlrPm2P45cZda-_qpQlmn_ksZiCzTRnpPGYG0U7AgtpoiUK85WTAX5CkH7jFpjYFap6LSSv33ujQtFFk52CECLu_iudN78IhIQri7z9TjtM4StCVCRS0Do6RoUrMm6kpd4rUbKD7LkTqbq3o7DOZU",
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8599827,
               "lng" : 151.2021282
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.8552624,
                  "lng" : 151.2031401
               },
               "southwest" : {
                  "lat" : -33.8657895,
                  "lng" : 151.2000123
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "92f1bbd4ecab8e9add032bccee40a57a8dfd42b4",
         "name" : "Barangaroo",
         "place_id" : "ChIJ1ZL9NkGuEmsRUEkzFmh9AQU",
         "reference" : "CpQBhQAAAE4kOaitGfNEGHyhPnHMiP5NVmC9UsUYoYyOtBcO7zKBMNHR07V90Ohq6AV8qEWIAy0xpMvzlEimcdzW0KwdtCbUpWYKXFhby57oPCCUbMiIy_9yxPhtmnzcWFOMZqWWUnVxpYOW6hHR_JbLUbFRPZCSYqMJ9VL6xpxAbjulKVJFmJP4enq7MyeMe8rO0lJRCRIQelNwAky9wPZz9iyjcT7-UhoUGMbpbFWZZeZDjpOxJyipeWTTRxk",
         "scope" : "GOOGLE",
         "types" : [ "locality", "political" ],
         "vicinity" : "Barangaroo"
      }]
}

我用这种方式模型     1.GooglePlacesModel是包含List结果的主要模型;和     2.PlacesMainModel包含List几何;再次根据需要使用的子模型     现在我能够在结果数组中接收字符串字段,如名称,图标,但是无法接收像位置这样的几何字段     我遇到了以下堆栈跟踪

GooglePlacesModel plcModel = gson.fromJson(resultedPLaces, GooglePlacesModel.class);
where resultedPLaces is the json string as it is above,




 googleapis.com:443][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
    com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@356dd6e9 failed to deserialized json object {"geometry":{"location":{"lat":-33.8599827,"lng":151.2021282}}} given the type com.google.gson.ParameterizedTypeImpl@53b59b06
        at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
        at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
        at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
        at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
        at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
        at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
        at com.google.gson.Gson.fromJson(Gson.java:568)
        at com.google.gson.Gson.fromJson(Gson.java:515)
        at com.google.gson.Gson.fromJson(Gson.java:484)
        at com.google.gson.Gson.fromJson(Gson.java:434)
        at com.google.gson.Gson.fromJson(Gson.java:406)
        at com.webapp.actions.api.places.NearByPlacesAction.getNrByPlaces(NearByPlacesAction.java:88)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:140)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:158)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:101)

1 个答案:

答案 0 :(得分:0)

2.PlacesMainModel包含List geometry;

Geometry不是JSONArray,所以你不能将它作为一个简单的嵌套json对象列表。

为Geometry创建另一个模型并使用它代替List Geometry。