【问题标题】:Mapping JSON with nested maps to objects将带有嵌套映射的 JSON 映射到对象
【发布时间】:2021-07-06 18:25:07
【问题描述】:

我有一个由类别组成的 JSON 结构。这些类别可以有不同数量的节点,并且这些节点内部也有一个节点图。此 JSON 的示例结构如下所示:

[
   {
      "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": []
   }
]

我已经编写了允许用对象表示这个 JSON 的类:

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

将这种类型的 JSON 映射到这些类的实例中的正确方法是什么?

【问题讨论】:

  • 您可以使用 json_serializable 包,您可以在这里找到详细信息:flutter.dev/docs/development/data-and-backend/json。顺便说一句,我认为您的节​​点属性应该是 List,而不是 Map
  • @Towelyey 你删除了这个问题的先前版本,你提到你使用 quicktype,io 来生成类。当然,它还生成了 fromJsontoJson 方法来将类映射到 JSON 和从 JSON 映射。那你为什么现在要这样的代码?
  • @PatrickO'Hara 老实说,flutter 对我来说是新事物,这是我应该做的第一件事。我不会撒谎,它让我不知所措,我问的第一个问题没有准确形成,因此我决定删除它并创建一个新问题,更加清楚我在寻找什么。因为我把上一个问题删了,稍后再创建这个问题,我忘记了这些 toJson 和 fromJson 函数是由 quicktype 实现的,所以我再次询问了这样的代码。

标签: json flutter mapping


【解决方案1】:

一个选项是遍历节点 List 并从每个节点创建一个 Node 类。在这里,我实现了一个 fromJson 命名构造函数,但存在 dart 包以简化反序列化过程,例如 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-27
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多