0

I have a JSON structure that consists of categories. These categories can have different amount of nodes and these nodes have a map of nodes inside of them aswell. The example structure of this JSON looks like this:

[
   {
      "id":"category1",
      "name":"Piłka nożna",
      "nodes":[ 
        {
            "id":"node1",
            "name":"Bayern Monachium",
            "parentId":"category1",
            "nodes": [
                {
                    "id":"node12",
                    "name":"Robert Lewandowski",
                    "parentId":"node1",
                    "nodes": []    
                },
                {
                    "id":"node13",
                    "name":"Thomas Mueller",
                    "parentId":"node1",
                    "nodes": []                
                }
            ]
         },
         {
            "id":"node2",
            "name":"Hertha Berlin",
            "parentId":"category1",
            "nodes": []
         },
         {
            "id":"node5",
            "name":"Werder Brema",
            "parentId":"category1",
            "nodes": []
         }
      ]
   },
   {
      "id":"category2",
      "name":"Koszykówka",
      "nodes": []
   }
]

I have written classes which would allow to represent this JSON with objects:

class Category {
  String id;
  String name;
  Map<String,Node> nodes;
  
  Category(String id, String name, Map<String,Node> nodes) {
    this.id = id;
    this.name = name;
    this.nodes = nodes;
  }
}

class Node {
  String id;
  String name;
  String parentId;
  Map<String, Node> nodes;
  
  Node(String id, String name, String parentId, Map<String, Node> nodes) {
    this.id = id;
    this.name = name;
    this.parentId = parentId;
    this.nodes = nodes;
  }
}

What would be the proper way to map the JSON of this type into the instances of these classes?

3
  • 3
    You can use json_serializable package, you can find details here: flutter.dev/docs/development/data-and-backend/json. Btw, I think your node property should be List<Node>, not Map<String,Node>. Commented Apr 11, 2021 at 13:38
  • @Towelyey you deleted the previous version of this question where you mentioned that you had used quicktype,io to generate the classes. Of course it also generated the methods fromJson and toJson to map the classes to and from JSON. So why are you now asking for such code? Commented Apr 11, 2021 at 13:47
  • @PatrickO'Hara To be honest flutter is something new to me and this is the first thing that I am supposed to do. I'm not gonna lie, it overwhelmed me and the first question that I have asked was not precisely formed therefore i decided to delete it and create a new one, being more aware what I am looking for. Because I have deleted the previous question and created this one a little bit later, I forgot that these toJson and fromJson functions were implemented by quicktype , hence I asked again for such code. Commented Apr 11, 2021 at 21:00

1 Answer 1

1

An option is to loop over nodes List and create a Node class from each one of them. Here, I implemented a fromJson nammed constructor but dart packages exist to ease the process of deserialization such as json_serializable.

class Node {
  String id;
  String name;
  String parentId;
  List<Node> nodes;
  
  Node.fromJson(Map<String, dynamic> json) {
    this.id = json["id"];
    this.name = json["name"];
    this.parentId = json["parentId"];
    this.nodes = json["nodes"].map<Node>((node) {
      return Node.fromJson(node);
    }).toList();
  }
}

class Category {
  String id;
  String name;
  List<Node> nodes;
  
  Category.fromJson(Map<String, dynamic> json) {
    this.id = json["id"];
    this.name = json["name"];
    this.nodes = json["nodes"].map<Node>((node) {
      return Node.fromJson(node);
    }).toList();
  }
}

  
final categories = [json list].map<Category>((category) => Category.fromJson(category)).toList();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.