0

I have an interface Employee and Department. I'm loading JSON from server that I need to "parse" to object that implements those interfaces. Is there a way how to achieve this automatically since all types in interface and JSON object are base types (String, number, list, map)?

// Abstract classes represents interfaces
abstract class Employee {
  String firstName;
  String lastName;
}

abstract class Department {
  String name;
  List<Employee> employees;
}

// JSON
{
  "name": "Development",
  "employees":
  [
    {"firstName":"John", "lastName":"Doe"}, 
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
  ]
}

I want to parse it like this

main() {
  ...
  Department department = someMethodToParse(jsonFromServer);
  ...
}
ondrej
  • 967
  • 7
  • 26

1 Answers1

3

There are a few packages that deal with JSON de/serialization

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567