I'm having a hard time figuring out how to parse a JSON response that has unique keys into nested object.
I've done some research and came across this article (https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51) but it doesn't give an example of a JSON object with unique/random keys.
See below for the json response I get from the backend. The student usernames are unique (i.e. john354, kim553, etc) and a new student could get added at any time. Could someone give me an example of what my model should look like for this type of structure and how to display the data with listview.builder? Thank you so much!
{
"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"
}
}
Attempt Data Model
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;
}
}
See Question&Answers more detail:
os