本文介绍了GSON抛出期望的BEGIN_ARRAY,但是BEGIN_OBJECT错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道这个错误的含义,但没有看到原因。



这是我的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,
块: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_pa​​rts:null,
qc:null
}
}]}

这是我的模型,由pojogenerator生成。

pre $ public元素DadataResponse {

@SerializedName(建议)
@Expose
私人列表< Suggestion> suggestions = new ArrayList< Suggestion>();

/ **
*
* @return
*建议
* /
public List< Suggestion> getSuggestions(){
返回建议;
}

/ **
*
* @参数建议
*建议
* /
public void setSuggestions列出<建议>建议){
this.suggestions = suggestions;
}

}

 public class Suggestion {

@SerializedName(value)
@Expose
private String value;
@SerializedName(unrestricted_value)
@Expose
private String unrestrictedValue;
@SerializedName(data)
@Expose
私有数据数据;
$ b $ **
*
* @return
*
* /
public String getValue(){
返回值;

$ b $ **

* @param value
*
* /
public void setValue(字符串值){
this.value = value;

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

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

$ b / **
*
* @return
*数据
* /
public Data getData() {
返回数据;
}

/ **
*
* @param data
*数据
* /
public void setData数据数据){
this.data = data;
}

}

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



那么为什么我收到此错误?请帮助

UPD

  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)字符串查询);

$ b $ / code>


解决方案

/ p>

  List< DadataResponse> getDadata(@Query(query)字符串查询); 

  DadataResponse getDadata(@Query(query)String query); 



表示期待一个数组(因为您告诉它),但它收到了一个对象。

看起来您收到一个 DadataResponse ,而不是它们的数组,因此您需要更新签名以符合现实。

I am using retrofit and parse result with GSON. Don't know why but I receive this one

I know what this error mean but don't see the reason.

This is my json response

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

This is my models generated by 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;
}

}

and the data class but its too big and don't need to post it here.

So why I receive this error? Please help

UPD

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

 }
解决方案

Change

List<DadataResponse> getDadata(@Query("query") String query);

to

DadataResponse getDadata(@Query("query") String query);

means it's expecting an array (because you told it to) but it received an object.

It looks like you receive a single DadataResponse and not an array of them, so you need to update your signature to match reality.

这篇关于GSON抛出期望的BEGIN_ARRAY,但是BEGIN_OBJECT错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:31