簡體   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