【发布时间】:2020-07-03 16:40:29
【问题描述】:
我的项目对我找不到解决方案的列表列表进行反序列化时遇到问题。我有三个类:具有引用和值的记录,具有名称和记录列表的实验,以及具有名称和实验列表的项目。我的代码如下所示。我有正确的 fromJson 用于记录和实验,但无法获得正确的 fromJson 用于项目。你能帮我吗 ?我的 toJson 项目也正确吗? 非常感谢,感谢您的帮助。
class Project {
final String name;
final List<Experiment> experiments;
Project({this.name, this.experiments});
factory Project.fromJson(Map<String, dynamic> json) {
}
Map<String, dynamic> toJson() => {
'name': name,
'experiments': experiments,
};
}
class Experiment {
String name;
List<Record> records;
Experiment({this.name, this.records});
factory Experiment.fromJson(Map<String, dynamic> json) {
var list = json['records'] as List;
List<Record> imagesList = list.map((i) => Record.fromJson(i)).toList();
return Experiment(records: imagesList, name: json['name']);
}
Map<String, dynamic> toJson() => {
'name': name,
'records': records,
};
}
class Record {
final double time;
final double value;
Record({this.time, this.value});
factory Record.fromJson(Map<String, dynamic> json) {
return Record(time: json['time'], value: json['value']);
}
Map<String, dynamic> toJson() => {
'time': time,
'value': value,
};
}`
【问题讨论】:
-
以下似乎确实有效:factory Project.fromJson(Map
json) { var list = json['experiments'] as List;打印(列表); List imagesList = list.map((i) => Experiment.fromJson(i)).toList();打印(' - - - ');打印(imagesList[2].records[1].value);返回项目(名称:json['name'],实验:imagesList); }
标签: json list flutter serialization deserialization