我正在尝试将对象列表从JSON转换为类结构。我遇到了一些我似乎无法解决的类型错误。
我已经建立了一些方法来将对象的每个部分与JSON相互转换,并且它们分别工作良好,而且我没有任何问题。问题是当我尝试用所有零件的清单一起做时。
我的代码出现错误的地方:
var temp = json.decode(response.body);
var shifts = ShiftList.fromJson(temp); // This is where it breaks
ShiftList类:
class ShiftList {
List<Shift> shifts;
ShiftList({
this.shifts,
});
addShift(Shift shift){
if(shifts == null){
shifts = [shift];
}else{
shifts.add(shift);
}
}
factory ShiftList.fromJson(Map<String, dynamic> json) {
return ShiftList(
shifts: _toObjectList(json['Shifts'], (e) => Shift.fromJson(e)),
);
}
Map<String, dynamic> toJson() {
return {
'Shifts': _fromList(shifts, (e) => e.toJson()),
};
}
}
List _fromList(data, Function(dynamic) toJson) {
if (data == null) {
return null;
}
var result = [];
for (var element in data) {
var value;
if (element != null) {
value = toJson(element);
}
result.add(value);
}
return result;
}
List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
if (data == null) {
return null;
}
var result = <T>[];
for (var element in data) {
T value;
if (element != null) {
value = fromJson(element as Map<String, dynamic>);
}
result.add(value);
}
return result;
}
班次班:
class Shift {
String Name;
String AccountID;
String Identifier;
UserList UserVal;
ReportList Reports;
TaskList Tasks;
String Scheduling;
String ShiftManagerCode;
Shift({
this.AccountID,
this.Name,
this.ShiftManagerCode,
this.Identifier,
this.UserVal,
this.Reports,
this.Tasks,
this.Scheduling,
});
factory Shift.fromJson(Map<String, dynamic> json) {
return Shift(
AccountID: json['AccountID'] as String,
Identifier: json['Identifier'] as String,
Name: json['ShiftName'] as String,
ShiftManagerCode: json['ShiftManagerCode'] as String,
UserVal: json['UserVal'] as UserList,
Reports: json['Reports'] as ReportList,
Tasks: json['Tasks'] as TaskList,
Scheduling: json['Scheduling'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'ShiftName': this.Name,
'AccountID': this.AccountID,
'Identifier': this.Identifier,
'ShiftManagerCode': this.ShiftManagerCode,
'UserVal': this.UserVal,
'Reports': this.Reports,
'Tasks': this.Tasks,
'Scheduling': this.Scheduling,
};
}
}
最后是出现错误的UserList类:
class UserList {
List<UserObject> users;
UserList({
this.users,
});
addUser(UserObject user){
if(users == null){
users = [user];
}else{
users.add(user);
}
}
factory UserList.fromJson(Map<String, dynamic> json) {
return UserList(
users: _toObjectList(json['UserVal'], (e) => UserObject.fromJson(e)),
);
}
Map<String, dynamic> toJson() {
return {
'UserVal': _fromList(users, (e) => e.toJson()),
};
}
}
List _fromList(data, Function(dynamic) toJson) {
if (data == null) {
return null;
}
var result = [];
for (var element in data) {
var value;
if (element != null) {
value = toJson(element);
}
result.add(value);
}
return result;
}
List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
if (data == null) {
return null;
}
var result = <T>[];
for (var element in data) {
T value;
if (element != null) {
value = fromJson(element as Map<String, dynamic>);
}
result.add(value);
}
return result;
}
我希望它可以将JSON对象放入我已有的类结构中。我得到的具体错误如下:
flutter: type 'List<dynamic>' is not a subtype of type 'UserList' in typecast
我的第一个代码段中的变量temp是以下内容:
{
"Shifts": [
{
"UserVal": [
{
"UID": "test",
"Email": "test@gmail.com",
"Phone": "0000000000",
"Name": "James"
},
{
"UID": "test",
"Email": "test@gmail.com",
"Phone": "0000000000",
"Name": "Jacob"
}
],
"AccountID": "1295",
"Identifier": "JhfSC",
"ShiftManagerCode": "15A4",
"Reports": [
{
"Quantity": "3",
"User": "Jacob",
"Supplies": "ItemA",
"Subject": "Supplies",
"Note": "test"
}
],
"Scheduling": "EMPTY",
"ShiftName": "AF 37",
"Tasks": [
{
"Status": "done",
"User": "James",
"Description": "Description here",
"Name": "TaskName here"
}
]
}
]
}
答案 0 :(得分:2)
让您移位类包含List的属性,而不是创建包含列表的新对象。
$data['name'] = "Guest";
Mail::send('emails.email', $data, function ($message) {
$message->to('email@gmail.com', 'name')
->subject('topic');
});
因此,现在您可以直接从Json进行内部转换。您必须在toJson中做一些额外的工作才能正确映射它。
答案 1 :(得分:0)
在不深入研究实现的情况下,json.decode(response.body)
返回了List<dynamic>
类型。您预计要使用List<Map<String, dynamic>
,因此您需要在列表中致电.cast<Map<String, dynamic>>()
。