【问题标题】:Parse JSON object as Interface in Dart在 Dart 中将 JSON 对象解析为接口
【发布时间】:2014-08-16 19:14:30
【问题描述】:

我有一个界面 Employee 和 Department。我正在从需要“解析”到实现这些接口的对象的服务器中加载 JSON。由于接口和 JSON 对象中的所有类型都是基本类型(字符串、数字、列表、映射),有没有办法自动实现这一点?

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

我想这样解析

main() {
  ...
  Department department = someMethodToParse(jsonFromServer);
  ...
}

【问题讨论】:

    标签: json interface dart


    【解决方案1】:
    猜你喜欢
    • 2021-05-17
    • 2022-01-09
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多