【发布时间】:2021-12-26 16:44:53
【问题描述】:
我的代码:
Future<List<Quections>?> getSiteQuection(String SiteId, SuperisorID) async {
var data = await http.post(
Uri.parse("https://churchiest-dump.000webhostapp.com/question.php"),
headers: {'Content-Type': 'application/json', 'Charset': 'utf-8'},
body: jsonEncode({
"site_id": SiteId,
"date": DateFormat.yMd().format(DateTime.now()),
"supervisor_id": SuperisorID
}));
if (data.statusCode == 200) {
print(data.body);
var jsonResponse = json.decode(data.body);
return (jsonResponse as List<dynamic>?)
?.map((e) => Quections.fromJson(e as Map<String, dynamic>))
.toList();
} else {
print("faild to response Quection list :" + data.body);
}
}
您还想知道下面插入的值是否可以用于snapshot.data [index].inserted 在 getSiteQuection Future 构建器中将其作为 Future 后。
class Quections {
Quections({
this.the0,
this.questionId = '',
this.question = '',
});
The0? the0;
String questionId;
String question;
factory Quections.fromRawJson(String str) =>
Quections.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Quections.fromJson(Map<String, dynamic> json) => Quections(
the0: The0.fromJson(json["0"]),
questionId: json["question_id"],
question: json["question"],
);
Map<String, dynamic> toJson() => {
"0": the0!.toJson(),
"question_id": questionId,
"question": question,
};
}
class The0 {
The0({
this.inserted = '',
});
String inserted;
factory The0.fromRawJson(String str) => The0.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory The0.fromJson(Map<String, dynamic> json) => The0(
inserted: json["inserted"],
);
Map<String, dynamic> toJson() => {
"inserted": inserted,
};
}
Json
{
"question_id": "2",
"question": "Test question"
}{
"question_id": "3",
"question": "is it clean?",
"0": {
"inserted": "1"
}
}{
"question_id": "4",
"question": "is it good looking?",
"0": {
"inserted": "1"
}
}
【问题讨论】:
-
你试过了吗?如果没有试试stackoverflow.com/questions/55671441/…
-
那个 JSON 确实是无效的。你不能有
{...}{...}= 即连续两个对象。这个无效 JSON 的来源是什么? -
@shanmkha 谢谢。该解决方案有效。但我得到另一个错误'发生异常。 _CastError(类型 'String' 不是类型转换中类型 'List
?' 的子类型)' -
尝试将“String”替换为“var”或List
标签: json flutter formatexception