2016-04-22 187 views
1

這是我的JSON預計BEGIN_ARRAY但BEGIN_OBJECT在行1列96路

{ 
    "success": true, 
    "error": null, 
    "responseA": { 
     "responseB": [{ 
        "response": {...}, 
        "request": "\/observations\/atlanta,ga" 
       }, { 
        "response": [{...}], 
        "request": "\/forecasts\/atlanta,ga" 
       }, 
       ... 
      ] 
    } 
} 

我創造我的課從這裏JSON: http://www.jsonschema2pojo.org/

,我覺得標題的誤差從數組和對象的兩個「響應」。

正如我所說的通過Retrofit來分析,我如何用Retrofit2.0分析兩種類型(數組和對象)的「repsonse」這樣的json?謝謝〜*

Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(BASE_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 
      .build();` 

這是日誌:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 96 path $.response.responses[0].response 
     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200) 
     at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40) 
     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81) 
     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) 
     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103) 
     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196) 
     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103) 
     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196) 
     at com.google.gson.Gson.fromJson(Gson.java:810) 
     at com.google.gson.Gson.fromJson(Gson.java:775) 
     at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:36) 
     at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:24) 
     at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:148) 
     at retrofit.OkHttpCall.execute(OkHttpCall.java:116) 
     at retrofit.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:61) 
+0

請張貼錯誤的logcat .... – sushildlh

+0

好像我改變從對象數組我的反應,它會說需要一個ARRY卻得到了一個對象.... – JoeKevin

+0

您的JSON開始與arrya – sushildlh

回答

1

有錯誤在你的JSON:

responseB第一對象具有響應對象和第二對象有響應陣列。這是創建問題

{ 

"success":true, 

"error":null, 

"responseA":{ 

    "responseB":[ 
    { 

     "response":{... ***// This is object*** 
       }, 
     "request":"\/observations\/atlanta,ga" 
    }, 
    { 
     "response":[ ***// This is Array*** 
      { ... 
       } 
     ], 
     "request":"\/forecasts\/atlanta,ga" 
    }, 
... ] 

} } 

您已經爲數組對象創建了相同的結構。 希望得到這個幫助。 感謝

+1

感謝您改進我的json格式,但是如何使用Retrofit 2.0分析「響應」有兩種類型的json? – JoeKevin

+0

對於這種情況,您必須手動解析,從Retrofit中獲取JsonObject。然後解析它,檢查它是否是json對象或數組。 –

相關問題