Java GSON抛出预期的BEGIN\u数组,但出现BEGIN\u对象错误
我正在对GSON使用改进和解析结果。不知道为什么,但我收到了这个 应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象 我知道这个错误的意思,但不明白原因 这是我的json响应Java GSON抛出预期的BEGIN\u数组,但出现BEGIN\u对象错误,java,android,gson,retrofit,Java,Android,Gson,Retrofit,我正在对GSON使用改进和解析结果。不知道为什么,但我收到了这个 应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象 我知道这个错误的意思,但不明白原因 这是我的json响应 {"suggestions": [ { "value": "г Казань", "unrestricted_value": "Респ Татарстан, г Казань", "data": { "qc_complete": null, "qc_house": null,
{"suggestions": [
{
"value": "г Казань",
"unrestricted_value": "Респ Татарстан, г Казань",
"data": {
"qc_complete": null,
"qc_house": null,
"qc_geo": "4",
"postal_code": "420000",
"postal_box": null,
"country": "Россия",
"region_fias_id": null,
"region_kladr_id": null,
"region_with_type": "Респ Татарстан",
"region_type": "Респ",
"region_type_full": "республика",
"region": "Татарстан",
"area_fias_id": null,
"area_kladr_id": null,
"area_with_type": null,
"area_type": null,
"area_type_full": null,
"area": null,
"city_fias_id": null,
"city_kladr_id": null,
"city_with_type": "г Казань",
"city_type": "г",
"city_type_full": "город",
"city": "Казань",
"city_district": null,
"settlement_fias_id": null,
"settlement_kladr_id": null,
"settlement_with_type": null,
"settlement_type": null,
"settlement_type_full": null,
"settlement": null,
"street_fias_id": null,
"street_kladr_id": null,
"street_with_type": null,
"street_type": null,
"street_type_full": null,
"street": null,
"house_fias_id": null,
"house_kladr_id": null,
"house_type": null,
"house_type_full": null,
"house": null,
"block_type": null,
"block_type_full": null,
"block": null,
"flat_area": null,
"square_meter_price": null,
"flat_price": null,
"flat_type": null,
"flat_type_full": null,
"flat": null,
"fias_id": "93b3df57-4c89-44df-ac42-96f05e9cd3b9",
"fias_level": "4",
"kladr_id": "1600000100000",
"tax_office": "1600",
"tax_office_legal": null,
"capital_marker": "2",
"okato": "92401000000",
"oktmo": "92701000",
"timezone": null,
"geo_lat": "55.7943051",
"geo_lon": "49.1116709",
"beltway_hit": null,
"beltway_distance": null,
"unparsed_parts": null,
"qc": null
}
} ]}
这是由pojogenerator生成的我的模型
public class DadataResponse {
@SerializedName("suggestions")
@Expose
private List<Suggestion> suggestions = new ArrayList<Suggestion>();
/**
*
* @return
* The suggestions
*/
public List<Suggestion> getSuggestions() {
return suggestions;
}
/**
*
* @param suggestions
* The suggestions
*/
public void setSuggestions(List<Suggestion> suggestions) {
this.suggestions = suggestions;
}
}
还有数据类,但是它太大了,不需要在这里发布
那么我为什么会收到这个错误呢?请帮忙
UPD
公共ApiService(字符串URLSTRING){
RequestInterceptor RequestInterceptor=新的RequestInterceptor(){
@凌驾
公共无效截获(请求门面请求){
请求.addHeader(“授权”、“令牌”);
addHeader(“内容类型”、“应用程序/json”);
addHeader(“接受”、“应用程序/json”);
}
};
RestAdapter RestAdapter=new RestAdapter.Builder()
.setEndpoint(URLSTRING)
.setRequestInterceptor(requestInterceptor)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
mApi=restAdapter.create(Api.class);
}
公共接口Api{
@获取(“/地址”)
List getDadata(@Query(“Query”)字符串查询);
}
更改
List<DadataResponse> getDadata(@Query("query") String query);
应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象
意味着它需要一个数组(因为您告诉它),但它收到了一个对象
看起来您收到的是一个
DadataResponse
,而不是一个数组,因此您需要更新签名以符合实际情况。这里缺少的部分是调用gson解析json的代码。你的类型标记可能是错的。只有一件事,伙计。试试看swift@njzk2我增加了改装call@vzamanillo情况正好相反(但是,是的,allready问了无数次)。。。显然,gson期望列表,但json是一个单一的对象。我并没有写道,suggestions
是一个单一的对象。。。根(DadataResponse
)是单个对象
public ApiService(String URLSTRING){
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Authorization", "Token");
request.addHeader("Content-Type","application/json");
request.addHeader("Accept","application/json");
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(URLSTRING)
.setRequestInterceptor(requestInterceptor)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
mApi = restAdapter.create(Api.class);
}
public interface Api{
@GET("/address")
List<DadataResponse> getDadata(@Query("query") String query);
}
List<DadataResponse> getDadata(@Query("query") String query);
DadataResponse getDadata(@Query("query") String query);