在Dart中使用唯一键解析JSON对象 [英] Parsing JSON Object with unique key in Dart

查看:59
本文介绍了在Dart中使用唯一键解析JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a hard time figuring out how to parse a JSON response that has unique keys into nested object.

我已经进行了一些研究,并发现了这篇文章( https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51 ),但它没有提供具有唯一/随机密钥的JSON对象的示例.

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.

有关从后端获得的json响应,请参见下文.学生的用户名是唯一的(即john354,kim553等),并且可以随时添加新学生.有人可以给我一个例子,说明我的模型对于这种类型的结构应该是什么样的,以及如何使用listview.builder显示数据吗?非常感谢!

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

尝试数据模型

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;
  }
}

推荐答案

要简化JSON转换,您可以使用 app.quicktype.io 将JSON转换为DART.

To simplify JSON pursing you can use app.quicktype.io to convert JSON to 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,
    };
}

要解析此JSON数据,请执行最终学校= schoolFromJson(jsonString);

To parse this JSON data, do final school = schoolFromJson(jsonString);

这篇关于在Dart中使用唯一键解析JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆