繁体   English   中英

Flutter 如何将嵌套的 JSON 数组插入到不同的 SQLite 表中

[英]Flutter How to insert nested JSON array into different SQLite tables

我希望我能清楚地解释我的需求,我拥有的是一个 API,我可以连接到它并从中接收 JSON。

Json 嵌套在一个列表和一个具有 2ed 层列表的对象中。

我想要的是将此 Josn 插入到我的本地数据库中(使用 SQLite for Flutter)。

我达到的点是插入 json 数据的第一层,但无法插入作为列表存在于 2ed 层中的数据。 虽然我将它序列化,但是当我尝试插入之后的列表时,没有从列表表单到数据库表字段的映射。

就像我有 [Class User] 和 [Class Company] 一个用户有很多公司。

当从 JSON 序列化数据时,我让数据进入用户类,然后嵌套到公司类,并为 json 对象的 2ed 层执行序列化,然后返回。

下一步是将数据插入数据库,但我在最后一个对象中的数据是这样的:

"username" : userName "company" : CompanyList --- 这是一个列表,不能以嵌套方式直接插入到数据库中。 由于公司列表有多个字段。 并且不止一个记录,例如:两家公司。

我相信会有一张地图可以反向执行此操作,因此我可以将其插入数据库(我在我的公司类中有它),但由于差异,我无法从尝试将数据插入数据库的地方调用它在 Classes 实例中

我手上的实例是一个User Type实例[两者之间的主Class],而companyList如果要映射它需要一个Company实例类型。

我可以提供代码或解释更多,有关该方法的任何帮助或想法都可能有所帮助。 非常感谢!

#EDIT 这是我尝试将数据插入数据库的地方

第一次插入很好,但第二次插入则不然。

  Future<int> createUserPermissions(UserPermissions userPermissions, ComListPermissions comListPermissions) async {
    final db = await dbProvider.database;
    var result = db.insert(userPermissionsDBTable, userPermissions.mapToDB());

    db.insert(userCompaniesTableDB, comListPermissions.toJson());
    return result;
  }

用户类

class UserPermissions {
  final String userName;
  final  List<ComListPermissions> comLists;
  final bool active;

  final String groupName;
  final int companyId;
  final String permissions;
  final ComListPermissions company;

  UserPermissions({this.company, this.groupName, this.companyId, this.permissions, this.userName, this.active, this.comLists});

  factory UserPermissions.mapFromJsonToDB(Map<String, dynamic> data) {
    if (data['comLists'] != null) {
      var companyObjJson = data['comLists'] as List;

      List<ComListPermissions> _companies = companyObjJson
          .map((companyJson) => ComListPermissions.fromJson(companyJson))
          .toList();

      return UserPermissions(
        userName: data['userName'],
        active: data['active'],
        comLists: _companies,
      );
    } else {
      return UserPermissions(
        userName: data['userName'],
        active: data['active'],
      );
    }
  }

  Map<String, dynamic> mapToDB() => {
        "userName": userName,
       // "comLists": comLists,
        "active": active,
      };

}

公司班级

class ComListPermissions {
  ComListPermissions({
    this.groupName,
    this.companyId,
    this.permissions,
  });

  String groupName;
  int companyId;
  String permissions;
  //List<String> permissions;



  factory ComListPermissions.fromJson(Map<String, dynamic> json) {
    if (json['permissions'] != null) {
      var permissionsObjJson = json['permissions'] as List;
      String s = permissionsObjJson.toString();

      return ComListPermissions(
        groupName: json["groupName"],
        companyId: json["companyID"],
        permissions: s,
      );
    } else {
      return ComListPermissions(
        groupName: json["groupName"],
        companyId: json["companyID"],
      );
    }
  }

  Map<String, dynamic> toJson() => {
        "groupName": groupName,
        "companyID": companyId,
        "permissions": permissions,

    //"permissions": List<dynamic>.from(permissions.map((x) => x)),
      };
}

我只是添加了一个 for 循环并在每次需要将新记录插入我的数据库时发送索引

  Future<int> createUserPermissions(UserPermissions userPermissions) async {
    final db = await dbProvider.database;
    var result = db.insert(userPermissionsDBTable, userPermissions.mapToDB());
    int index = userPermissions.comLists.length;
    for (int i = 0; i < index; i++) {
      db.insert(userCompaniesTableDB, userPermissions.toDatabaseForCompany(i));
    }
    return result;
  }
  Map<String, dynamic> toDatabaseForCompany(int index) => {
    "companyID": comLists[index].companyId,
    "groupName": comLists[index].groupName,
    "permissions": comLists[index].permissions,
  };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM