java - GSON 抛出 Expected BEGIN_ARRAY but was BEGIN_OBJECT 错误

标签 java android gson retrofit

<分区>

我正在使用 GSON 进行改造和解析结果。不知道为什么,但我收到了这个

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path

我知道这个错误是什么意思,但不明白原因。

这是我的 json 响应

{"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;
}

public class Suggestion {

@SerializedName("value")
@Expose
private String value;
@SerializedName("unrestricted_value")
@Expose
private String unrestrictedValue;
@SerializedName("data")
@Expose
private Data data;

/**
 * 
 * @return
 *     The value
 */
public String getValue() {
    return value;
}

/**
 * 
 * @param value
 *     The value
 */
public void setValue(String value) {
    this.value = value;
}

/**
 * 
 * @return
 *     The unrestrictedValue
 */
public String getUnrestrictedValue() {
    return unrestrictedValue;
}

/**
 * 
 * @param unrestrictedValue
 *     The unrestricted_value
 */
public void setUnrestrictedValue(String unrestrictedValue) {
    this.unrestrictedValue = unrestrictedValue;
}

/**
 * 
 * @return
 *     The data
 */
public Data getData() {
    return data;
}

/**
 * 
 * @param data
 *     The data
 */
public void setData(Data data) {
    this.data = data;
}

还有数据类,但它太大了,不需要在这里发布。

那么为什么我收到这个错误?请帮忙

更新

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);

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path

意味着它期待一个数组(因为你告诉它)但它收到了一个对象。

您似乎收到了一个 DadataResponse 而不是它们的数组,因此您需要更新您的签名以符合实际情况。

关于java - GSON 抛出 Expected BEGIN_ARRAY but was BEGIN_OBJECT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37256201/

相关文章:

java - 在属性文件中保存特殊字符

Java从config.yml中获取Maps的List of maps

java - 尝试在 fragment 中的recyclerview中打开 Activity 时,应用程序崩溃

android - GridView 的高度是如何确定的,是否可以更改?

java - 尝试更新 cocos OpenSSL 时出错

java - Gson 和 SharedPreferences 导致 NullPointerException

java - 使用时间戳会导致 WSDL 生成出现问题

android关于从沙箱中读取的文件

android - 是否可以在 Room 中忽略基本更新中的字段

java - 如何让Gson的fromJson创建对象的arraylilst