1

Пытаюсь десериализовать JSON ответ с помощью GSON через
response ResponseFio = gson.fromJson(json, response.class), выпадает ошибка:

Expected BEGIN_ARRAY but was BEGIN_OBJECT

Через JsonParser все получается, но через класс нет, подозрение на неправильный тип данных в классе

JSON:

{
"response": {
"count": 3,
"items": [
  {
    "id": 100,
    "first_name": "Иван",
    "last_name": "Иванов"
  },
  {
    "id": 101,
    "first_name": "Иван",
    "last_name": "Петров"
  },
  {
    "id": 102,
    "first_name": "Алла",
    "last_name": "Петрова"
  }
 ]
}
}

Класс response:

public class response {

List<GetFIO> response = null;

public response(){

}

public response(List<GetFIO> response){
    this.response = response;
}

public List<GetFIO> GetFriend(){
    return response;}
}

Класс GetFIO:

public class GetFIO {
int count = 0;
List<items> items = null;

public GetFIO(){

}

public GetFIO(int count, List<items> items){
    this.count = count;
    this.items = items;
}

public List<items> getItems() {
    return items;
}
}

Класс items:

public class items {
int id = 0;
String first_name = "",
        last_name = "";

public items(){

}

public items(int id, String first_name, String last_name){
    this.id = id;
    this.first_name = first_name;
    this.last_name = last_name;
}

public int getId() {
    return id;
}

public String getFirst_name() {
    return first_name;
}

public String getLast_name() {
    return last_name;
}
}
2
  • Expected BEGIN_ARRAY but was BEGIN_OBJECT- у вас десериализатор ожидает массив, а получает объект. List<GetFIO> response = null; у класса response.
    – user218976
    16 авг 2018 в 10:11
  • А пробовали изучить jdk.nashorn.internal.parser.JSONParser; и смотреть примеры?
    – And
    16 авг 2018 в 12:22

1 ответ 1

0

Попробуйте поменять тип на

GetFIO response = null;

т.е. когда вы десериализуете, то у вас "response" является объектом в json, а не массивом.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.