【发布时间】:2021-02-19 18:18:20
【问题描述】:
我很难弄清楚如何将具有唯一键的 JSON 响应解析为嵌套对象。
我进行了一些研究并看到了这篇文章 (https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51),但它没有给出具有唯一/随机键的 JSON 对象的示例。
我从后端得到的 json 响应见下文。学生用户名是唯一的(即 john354、kim553 等),可以随时添加新学生。有人可以给我一个例子,说明我的模型对于这种类型的结构应该是什么样的,以及如何使用 listview.builder 显示数据?非常感谢!
{
"school": {
"students": {
"john354": {
"fullName": "John Kindle",
"permissions": {
"permission1": "here",
"permission2": "here"
}
},
"kim553": {
"fullName": "Kim Johnson"
"permissions": {
"permission1": "here",
"permission2": "here"
}
},
"dory643": {
"fullName": "Dory Thomas"
"permissions": {
"permission1": "here",
"permission2": "here"
}
}
},
"footer": "Text goes here"
},
"status": {
"state": "open"
}
}
尝试数据模型
class School {
School school;
Status status;
School({this.school, this.status});
School.fromJson(Map<String, dynamic> json) {
school =
json['school'] != null ? new School.fromJson(json['school']) : null;
status =
json['status'] != null ? new Status.fromJson(json['status']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.school != null) {
data['school'] = this.school.toJson();
}
if (this.status != null) {
data['status'] = this.status.toJson();
}
return data;
}
}
class School {
Students students;
String footer;
School({this.students, this.footer});
School.fromJson(Map<String, dynamic> json) {
students = json['students'] != null
? new Students.fromJson(json['students'])
: null;
footer = json['footer'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.students != null) {
data['students'] = this.students.toJson();
}
data['footer'] = this.footer;
return data;
}
}
class Students {
final String username;
final Info info;
Options({this.username,this.info});
factory Students.fromJson(String name, Map<String, dynamic> json){
return Students(
username: username,
info: Info(
fullName: json['info']['fullName']
)
);
}
}
class Info {
String fullName;
Permissions permissions;
Info({this.fullName, this.permissions});
Info.fromJson(Map<String, dynamic> json) {
fullName = json['fullName'];
permissions = json['permissions'] != null
? new Permissions.fromJson(json['permissions'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['fullName'] = this.fullName;
if (this.permissions != null) {
data['permissions'] = this.permissions.toJson();
}
return data;
}
}
class Permissions {
String permission1;
String permission2;
Permissions({this.permission1, this.permission2});
Permissions.fromJson(Map<String, dynamic> json) {
permission1 = json['permission1'];
permission2 = json['permission2'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['permission1'] = this.permission1;
data['permission2'] = this.permission2;
return data;
}
}
class Status {
String state;
Status({this.state});
Status.fromJson(Map<String, dynamic> json) {
state = json['state'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['state'] = this.state;
return data;
}
}
【问题讨论】:
-
然后发布您现有的解析代码
-
@pskink 已更新数据模型
标签: json flutter parsing dart model