【发布时间】:2020-02-25 06:16:31
【问题描述】:
我想弄清楚这一点,因为这是一件非常简单的事情,而且在任何地方都没有相关信息。
我有一个返回 Student 的 API 调用,其定义如下
class Student {
String photo;
String upn;
String fullName;
String studentClass;
String studentYear;
}
我正在使用 dart http 库来调用返回一组学生的 API。我要做的就是将 API 返回的字符串反序列化为一个类型化的列表,这样我就可以用它做一些事情了。
studentFuture.then((res) {
log(res.body.toString());
List<dynamic> dynamicList = jsonDecode(res.body);
var student = dynamicList[0] as Student;
});
尝试使用 as 将其投射给 Student 不起作用,我只是明白了
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Student' in type cast
我想要的只是能够像在 C# 中那样做这样的事情
var obj = DeserialiseJson<Type>(jsonString);
无论我尝试什么,我都无法将字符串反序列化为对象,我该怎么做?
【问题讨论】: