A
A
AlekseySheb2019-09-09 18:24:34
Java
AlekseySheb, 2019-09-09 18:24:34

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ why does the error occur?

Please help with Retrofit. Why does it give an error?

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

json piece:
{
  "city": [
    {
      "city_id": "6",
      "country_id": "4",
      "region_id": "5",
      "name": "Балларат"
    },
    {
      "city_id": "7",
      "country_id": "4",
      "region_id": "5",
      "name": "Бендиго"
    },
    {
      "city_id": "8",
      "country_id": "4",
      "region_id": "5",
      "name": "Варрнамбул"
    }
  ]
}

All json
Code:
class Cities {
  @SerializedName("city")
  private List city;
  public List getCity() {
    return city;
  }
}

class CityValue {
  @SerializedName("city_id")
  @Expose
  private String city_id;

  @SerializedName("counter_id")
  @Expose
  private String counter_id;

  @SerializedName("region_id")
  @Expose
  private String region_id;

  @SerializedName("name")
  @Expose
  private String name;
}

There is an array here, what's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Vlasov, 2019-09-09
@Neikist

Try to explicitly specify private List<CityValue> city;
Although not 100% sure, you can still try an array and not a list.
By the way, the link to JSON is not opening.

T
terminator-light, 2019-09-10
@terminator-light

Expected BEGIN_ARRAY but was BEGIN_OBJECT

indicates that an array was expected, but an object was received.
In other words, it was expected List<T>, but the object was received List.
So parametrize the List:
private List<CityValue> city;
public List<CityValue> getCity() {
    return city;
}
public void setCity(List<CityValue> city) {
    this.city = city;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question