Нужно настроить десериализацию Gson, нужен только один элемент JSON, чтобы Object

1

У меня есть: Json:

{
  "id" : "1",
  "name": "John",
  "id_client": "2",
  ...
 }

Мои занятия:

public class Person{
    private int id;
    private String name; 
    private Client client;
    ...
}

public class Client{
  private int id;
}

Как я десериализую id_client: "2", для объекта Client и set id (CLient)?

Теги:
gson

1 ответ

0
Лучший ответ
    ...
    Map<String,Object> parsed = jsonParser.parse(jsonString);
    Person thePerson = new Person(parsed);
    ...    

public class Person {
    ...
    public Person(Map<String,Object> parsed) {
        id = Integer.getInteger(parsed.getString("id"));
        name = parsed.getString("name");
        int idClient = Integer.getInteger(parsed.getString("id_client"));
        client = new Client(idClient);
    }
    ...
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню