预期BEGIN_ARRAY但在第1行第96列路径处是BEGIN_OBJECT

时间:2016-04-22 08:04:40

标签: android json retrofit2

这是我的json

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

我从这里为json创建了我的类: http://www.jsonschema2pojo.org/

我发现标题的错误来自两个“响应”,即数组和对象。

正如我所说通过Retrofit Ianalyze,我如何用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)

1 个答案:

答案 0 :(得分: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"
     },
... ]

} }

您为数组对象创建了相同的结构。 希望这有帮助。 感谢