四、在线自动转换
除了转为 Dart 类型之外 , 其它 语言 类型 也可以转换 , https://www.bejson.com/json2javapojo/new/ 网站可以 JSON 转 JavaBean ;
推荐一个 JSON 转 Dart 的工具网站 : https://jsontodart.com/
这是系统根据 JSON 字符串自动生成的 Dart 类 ;
class Autogenerated { String school; List<Students> students; Autogenerated({this.school, this.students}); Autogenerated.fromJson(Map<String, dynamic> json) { school = json['school']; if (json['students'] != null) { students = new List<Students>(); json['students'].forEach((v) { students.add(new Students.fromJson(v)); }); } } Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['school'] = this.school; if (this.students != null) { data['students'] = this.students.map((v) => v.toJson()).toList(); } return data; } } class Students { String name; String age; Students({this.name, this.age}); Students.fromJson(Map<String, dynamic> json) { name = json['name']; age = json['age']; } Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['name'] = this.name; data['age'] = this.age; return data; } }