一直在与JSON反序列化进行斗争。
杰森:
[
{
"question": {
"text": "Some question",
"url": "Some URL"
},
"answer": {
"text": "Some answer",
"url": "Some URL"
},
"options": [
{
"text": "Some option",
"url": "Some URL"
},
{
"text": "Some option",
"url": "https://itanium21.github.io/quizGame/option_1.mp3"
}
],
"fact": {
"text": "Some fact",
"url": "Some URL"
}
}
]代码:
class Recording {
final String text;
final String url;
Recording(this.text, this.url);
factory Recording.fromJson(Map<String, dynamic> json) {
return Recording(json['text'] as String, json['url'] as String);
}
@override
String toString() {
return '{ ${this.text}, ${this.url} }';
}
}
class Question {
final Recording question;
final Recording answer;
final List<Recording> options;
final Recording fact;
Question(
{required this.question,
required this.answer,
required this.options,
required this.fact});
factory Question.fromJson(Map<String, dynamic> json) {
var optionObjsJson = json['options'] as List;
List<Recording> optionObjs = optionObjsJson
.map((optionJson) => Recording.fromJson(optionJson))
.toList();
return Question(
question: json['question'],
answer: json['answer'],
options: optionObjs,
fact: json['fact']);
}
}应用程序中显示的错误:类型'IntetnalLinkedHashMap‘不是'Recording'.的子类型
有什么想法吗?尝试遵循本教程:https://bezkoder.com/dart-flutter-parse-json-string-array-to-object-list/
我也试过:
List<Recording> optionObjs = optionObjsJson.map((i) => Recording.fromJson(i)).toList();不幸的是来自https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51的同样的结果
发布于 2021-06-05 19:31:07
该死的!很棒的资源!谢谢,这对我有帮助!
https://stackoverflow.com/questions/67847314
复制相似问题