Home > Net >  Flutter/Dart Parsing JSON to model with a Map attribute
Flutter/Dart Parsing JSON to model with a Map attribute

Time:05-09

My API returns me this kind of json object where zones keys are dynamic

{
    "workspaces": [
        {
            "id": "1154237e8b6-7270-4c99-81e9-9d752e0c295c",
            "createdAt": "2022-05-08T15:43:25.440Z",
            "updatedAt": "2022-05-08T15:43:25.440Z",
            "siteId": "42cb1511-e836-45ed-9b56-034668b8f05a",
            "orgId": "bb46311b-b76c-4f6a-9726-65ab5730b69b",
            "name": "RDC"
        }
    ],
    "zones": {
        "134528b6-7270-4c99-81e9-9d752e0c295c": [
            {
                "id": "71a11d9e-e211-472b-a16d-861737c57ecd",
                "createdAt": "2022-05-08T15:43:25.451Z",
                "updatedAt": "2022-05-08T15:43:25.451Z",
                "siteId": "421158b-e836-45ed-9b56-034668b8f05a",
                "orgId": "bb4631b-b76c-4f6a-9726-65ab5730b69b",
                "workspaceId": "13a7e8b6-7270-4c99-81e9-9d752e0c295c",
                "name": "Auditorium"
            }
        ],
        "15a745326-7270-4c99-81e9-9d752e0c295c": [
            {
                "id": "71ad31e-e211-472b-a16d-861737c57ecd",
                "createdAt": "2022-05-08T15:43:25.451Z",
                "updatedAt": "2022-05-08T15:43:25.451Z",
                "siteId": "42cb158b-e836-45ed-9b56-034668b8f05a",
                "orgId": "b11b8b-b76c-4f6a-9726-65ab5730b69b",
                "workspaceId": "1318b6-7270-4c99-81e9-9d752e0c295c",
                "name": "Auditorium"
            }
        ]
    }
}

I would like to have a model that converts a json to a SiteHierarchyModel I'm struggling to convert this kind of attributes Map<String, List<ZoneModel>>

class SiteHierarchyModel {
  List<WorkspaceModel> workspaces;
  Map<String, List<ZoneModel>> zones;

  SiteHierarchyModel({
    required this.workspaces,
    required this.zones,
  });

  Map<String, dynamic> toJson() => {
        'workspaces': workspaces,
        'zones': zones,
      };

  SiteHierarchyModel.fromJson(Map<String, dynamic> json)
      : workspaces = List<WorkspaceModel>.from(json['workspaces']
            .map((workspace) => WorkspaceModel.fromJson(workspace))),
        zones = json['zones']; // THIS LINE
}

CodePudding user response:

You can also try it on https://app.quicktype.io/. This site easily converts your JSON to the dart model class. there are many different languages supported

import 'dart:convert';

SiteHierarchyModel siteHierarchyModelFromJson(String str) => SiteHierarchyModel.fromJson(json.decode(str));

String siteHierarchyModelToJson(SiteHierarchyModel data) => json.encode(data.toJson());

class SiteHierarchyModel {
    SiteHierarchyModel({
        this.workspaces,
        this.zones,
    });

    List<Workspace> workspaces;
    Map<String, List<Workspace>> zones;

    factory SiteHierarchyModel.fromJson(Map<String, dynamic> json) => SiteHierarchyModel(
        workspaces: List<Workspace>.from(json["workspaces"].map((x) => Workspace.fromJson(x))),
        zones: Map.from(json["zones"]).map((k, v) => MapEntry<String, List<Workspace>>(k, List<Workspace>.from(v.map((x) => Workspace.fromJson(x))))),
    );

    Map<String, dynamic> toJson() => {
        "workspaces": List<dynamic>.from(workspaces.map((x) => x.toJson())),
        "zones": Map.from(zones).map((k, v) => MapEntry<String, dynamic>(k, List<dynamic>.from(v.map((x) => x.toJson())))),
    };
}

class Workspace {
    Workspace({
        this.id,
        this.createdAt,
        this.updatedAt,
        this.siteId,
        this.orgId,
        this.name,
        this.workspaceId,
    });

    String id;
    DateTime createdAt;
    DateTime updatedAt;
    String siteId;
    String orgId;
    String name;
    String workspaceId;

    factory Workspace.fromJson(Map<String, dynamic> json) => Workspace(
        id: json["id"],
        createdAt: DateTime.parse(json["createdAt"]),
        updatedAt: DateTime.parse(json["updatedAt"]),
        siteId: json["siteId"],
        orgId: json["orgId"],
        name: json["name"],
        workspaceId: json["workspaceId"] == null ? null : json["workspaceId"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "createdAt": createdAt.toIso8601String(),
        "updatedAt": updatedAt.toIso8601String(),
        "siteId": siteId,
        "orgId": orgId,
        "name": name,
        "workspaceId": workspaceId == null ? null : workspaceId,
    };
}
  • Related