【问题标题】:Parsing JSON to a model in Flutter在 Flutter 中将 JSON 解析为模型
【发布时间】:2019-09-23 01:57:31
【问题描述】:

我有一些存储在 JSON 中的数据,我正在尝试将其映射到其各自的模型类。

{ 
"1":{ 
  "answer":"This is question 1",
  "question":"This is answer 1"
},
"2":{ 
  "answer":"This is question 2",
  "question":"This is answer 2"
},
"3":{ 
  "answer":"This is question 3",
  "question":"This is answer 3"
},
"4":{ 
  "answer":"This is question 4",
  "question":"This is answer 4"
},
"5":{ 
  "answer":"This is question 5",
  "question":"This is answer 5"
   }
}

我想将questionanswer 字段映射到模型,但是当它们嵌套在一个数字上时我该如何实现呢?通常我会创建另一个父类的模型类,但是当父类没有标记时你会怎么做?

class FaqModel {
  String question;
  String answer;

  FaqModel({this.question, this.answer});

  FaqModel.fromJson(Map<String, dynamic> json) {
    question = json["question"];
    answer = json["answer"];
  }
}

【问题讨论】:

标签: json flutter dart


【解决方案1】:

只需获取密钥并进行迭代,如下所示:


    final data = { 
"1":{ 
  "answer":"This is question 1",
  "question":"This is answer 1"
},
"2":{ 
  "answer":"This is question 2",
  "question":"This is answer 2"
},
"3":{ 
  "answer":"This is question 3",
  "question":"This is answer 3"
},
"4":{ 
  "answer":"This is question 4",
  "question":"This is answer 4"
},
"5":{ 
  "answer":"This is question 5",
  "question":"This is answer 5"
   }
};


  final models = data.keys.map((key) {
    final subData = data[key];
    return FaqModel.fromJson(subData);
  });

 models.forEach((item) {
   print("FAQ: ${item.question} \n ${item.answer}\n\n");
 });

【讨论】:

    【解决方案2】:

    只使用下面的模型

    // To parse this JSON data, do
    //
    //     final faqModel = faqModelFromJson(jsonString);
    
    import 'dart:convert';
    
    Map<String, FaqModel> faqModelFromJson(String str) =>     Map.from(json.decode(str)).map((k, v) => MapEntry<String, FaqModel>(k,     FaqModel.fromJson(v)));
    
    String faqModelToJson(Map<String, FaqModel> data) =>     json.encode(Map.from(data).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())));
    
    class FaqModel {
        String answer;
        String question;
    
        FaqModel({
            this.answer,
            this.question,
        });
    
        factory FaqModel.fromJson(Map<String, dynamic> json) => FaqModel(
            answer: json["answer"] == null ? null : json["answer"],
            question: json["question"] == null ? null : json["question"],
        );
    
        Map<String, dynamic> toJson() => {
            "answer": answer == null ? null : answer,
            "question": question == null ? null : question,
        };
    }
    

    你的 JSON

    final jsonData = { 
    "1":{ 
      "answer":"This is question 1",
      "question":"This is answer 1"
    },
    "2":{ 
      "answer":"This is question 2",
      "question":"This is answer 2"
    },
    "3":{ 
      "answer":"This is question 3",
      "question":"This is answer 3"
    },
    "4":{ 
      "answer":"This is question 4",
      "question":"This is answer 4"
    },
    "5":{ 
      "answer":"This is question 5",
      "question":"This is answer 5"
    }
    };
    

    并解析 JSON

    FaqModel faqModel = faqModelFromJson(jsonData);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-31
      • 2021-05-17
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 2021-07-20
      • 2019-10-23
      相关资源
      最近更新 更多