在Dart中使用唯一键解析JSON对象

在Dart中使用唯一键解析JSON对象,json,flutter,parsing,dart,model,Json,Flutter,Parsing,Dart,Model,我很难弄清楚如何将具有唯一键的JSON响应解析到嵌套对象中 我做了一些研究,发现了这篇文章(),但它没有给出带有唯一/随机键的JSON对象的示例 下面是我从后端得到的json响应。学生用户名是唯一的(即john354、kim553等),新学生可以随时添加。有人能给我举个例子,说明我的模型对于这种类型的结构应该是什么样的,以及如何使用listview.builder显示数据吗?非常感谢你 { "school": { "students"

我很难弄清楚如何将具有唯一键的JSON响应解析到嵌套对象中

我做了一些研究,发现了这篇文章(),但它没有给出带有唯一/随机键的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;
  }
}
班级学校{
学校;
地位;
学校({this.School,this.status});
School.fromJson(映射json){
学校=
json['school']!=null?新学校。fromJson(json['school']):null;
地位=
json['status']!=null?新状态。fromJson(json['status']):null;
}
映射到JSON(){
最终地图数据=新地图();
如果(this.school!=null){
data['school']=this.school.toJson();
}
如果(this.status!=null){
data['status']=this.status.toJson();
}
返回数据;
}
}
班级学校{
学生;
字符串页脚;
学校({this.students,this.footer});
School.fromJson(映射json){
students=json['students']!=null
?newstudents.fromJson(json['Students'])
:null;
footer=json['footer'];
}
映射到JSON(){
最终地图数据=新地图();
如果(this.students!=null){
data['students']=this.students.toJson();
}
数据['footer']=this.footer;
返回数据;
}
}
班级学生{
最终字符串用户名;
最终信息;
选项({this.username,this.info});
fromJson(字符串名,映射json){
留学生(
用户名:用户名,
信息:信息(
全名:json['info']['fullName']
)
);
} 
}
班级信息{
字符串全名;
权限;
信息({this.fullName,this.permissions});
Info.fromJson(映射json){
fullName=json['fullName'];
permissions=json['permissions']!=null
?新权限。fromJson(json['Permissions'])
:null;
}
映射到JSON(){
最终地图数据=新地图();
数据['fullName']=this.fullName;
if(this.permissions!=null){
data['permissions']=this.permissions.toJson();
}
返回数据;
}
}
类权限{
字符串权限1;
字符串许可证2;
权限({this.permission1,this.permission2});
Permissions.fromJson(映射json){
permission1=json['permission1'];
permission2=json['permission2'];
}
映射到JSON(){
最终地图数据=新地图();
数据['permission1']=this.permission1;
数据['permission2']=this.permission2;
返回数据;
}
}
阶级地位{
字符串状态;
状态({this.state});
Status.fromJson(映射json){
state=json['state'];
}
映射到JSON(){
最终地图数据=新地图();
数据['state']=this.state;
返回数据;
}
}

要简化JSON购买,您可以使用它将JSON转换为DART

尝试以下数据模型

import 'dart:convert';

School schoolFromJson(String str) => School.fromJson(json.decode(str));

String schoolToJson(School data) => json.encode(data.toJson());

class School {
    School({
        this.school,
        this.status,
    });

    final SchoolClass school;
    final Status status;

    factory School.fromJson(Map<String, dynamic> json) => School(
        school: SchoolClass.fromJson(json["school"]),
        status: Status.fromJson(json["status"]),
    );

    Map<String, dynamic> toJson() => {
        "school": school.toJson(),
        "status": status.toJson(),
    };
}

class SchoolClass {
    SchoolClass({
        this.students,
        this.footer,
    });

    final Map<String, Student> students;
    final String footer;

    factory SchoolClass.fromJson(Map<String, dynamic> json) => SchoolClass(
        students: Map.from(json["students"]).map((k, v) => MapEntry<String, Student>(k, Student.fromJson(v))),
        footer: json["footer"],
    );

    Map<String, dynamic> toJson() => {
        "students": Map.from(students).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
        "footer": footer,
    };
}

class Student {
    Student({
        this.fullName,
        this.permissions,
    });

    final String fullName;
    final Permissions permissions;

    factory Student.fromJson(Map<String, dynamic> json) => Student(
        fullName: json["fullName"],
        permissions: Permissions.fromJson(json["permissions"]),
    );

    Map<String, dynamic> toJson() => {
        "fullName": fullName,
        "permissions": permissions.toJson(),
    };
}

class Permissions {
    Permissions({
        this.permission1,
        this.permission2,
    });

    final String permission1;
    final String permission2;

    factory Permissions.fromJson(Map<String, dynamic> json) => Permissions(
        permission1: json["permission1"],
        permission2: json["permission2"],
    );

    Map<String, dynamic> toJson() => {
        "permission1": permission1,
        "permission2": permission2,
    };
}

class Status {
    Status({
        this.state,
    });

    final String state;

    factory Status.fromJson(Map<String, dynamic> json) => Status(
        state: json["state"],
    );

    Map<String, dynamic> toJson() => {
        "state": state,
    };
}

导入'dart:convert';
School-schoolFromJson(String-str)=>School.fromJson(json.decode(str));
字符串schoolToJson(学校数据)=>json.encode(data.toJson());
班级学校{
学校({
这个学校,,
这个,身份,,
});
期末班;
最后地位;
工厂学校。fromJson(MapJSON)=>学校(
学校:SchoolClass.fromJson(json[“学校]),
status:status.fromJson(json[“status”]),
);
映射到JSON()=>{
“school”:school.toJson(),
“status”:status.toJson(),
};
}
班级{
班级({
这个,学生们,
这个.footer,
});
最终地图学生;
最后的字符串页脚;
factory SchoolClass.fromJson(映射json)=>SchoolClass(
学生:Map.from(json[“students”]).Map((k,v)=>MapEntry(k,Student.fromJson(v)),
页脚:json[“页脚”],
);
映射到JSON()=>{
“学生”:Map.from(students.Map((k,v)=>MapEntry(k,v.toJson()),
“页脚”:页脚,
};
}
班级学生{
学生({
这个,全名,
这是权限,
});
最终字符串全名;
最终权限;
factory Student.fromJson(映射json)=>Student(
全名:json[“全名”],
权限:permissions.fromJson(json[“permissions”]),
);
映射到JSON()=>{
“全名”:全名,
“permissions”:permissions.toJson(),
};
}
类权限{
权限({
这是许可证1,
这是许可证2,
});
最终字符串许可证1;
最终字符串许可证2;
工厂权限。fromJson(映射json)=>权限(
permission1:json[“permission1”],
permission2:json[“permission2”],
);
映射到JSON()=>{
“许可证1”:许可证1,
“许可证2”:许可证2,
};
}
阶级地位{
地位({
这个州,
});
最终字符串状态;
工厂状态。fromJson(映射json)=>状态(
状态:json[“状态”],
);
映射到JSON()=>{
“国家”:国家,
};
}
要解析此JSON数据,请执行以下操作
final school=schoolFromJson(jsonString)

发布现有的解析代码then@pskink用数据模型更新