![](/img/trans.png)
[英]Can't print JSON data into Dart Objects - "Instance of 'Object'"
[英]JSON to Dart conversion errors: doesn't print
我正在嘗試從 JSON 響應中獲取數據。 這是我的樣本 JSON 數據:
{
"id": 97,
"parentWo": "2022-178348",
"parentWoDesc": "COMMERCIAL - BUSINESS ESTABLISHMENT - LUMBIA HOSPITAL - PN 21.0130",
"parentWoStatus": "APPR",
"parentWoStatusDate": "2022-03-22T13:36:00+08:00",
"parentWoWorkType": "CP",
"parentWoClass": "COMM",
"parentWoCrew": "",
"parentWoLead": "",
"parentWoLocation": "210130",
"parentWoDepartmentDescription": "Line Maintenance Department",
"parentWoSectionDescription": "Line Section",
"status": "I",
"approvedBy": "eams.extension2",
"approvedDate": "2022-07-21T09:59:09.727428+08:00",
"disapprovedBy": null,
"disapprovedDate": null,
"issuedBy": "eams.extension2",
"issuedDate": "2022-07-21T11:19:39.717066+08:00",
"voidedBy": null,
"voidedDate": null,
"receivedBy": "ALEX",
"receivedDate": "2022-07-21T11:19:39.717073+08:00",
"children": [
{
"id": 27,
"woNum": "2022-178349",
"woDesc": "Installation of POLE, CON, SPUN TY, (45) 14M X 500 [Rocky]",
"woStatus": "APPR",
"woStatusDate": "2022-03-22T13:36:01+08:00",
"woCrew": "",
"woLead": "",
"woLocation": "212636",
"materials": [
{
"id": 36,
"itemNum": "2001409",
"itemDescription": "POLE, CON, SPUN TY, (45) 14M X 500",
"itemQuantityPlan": 1.0,
"itemQuantityIssued": 1.0,
"itemWoBalance": 0.0,
"itemInventoryBalance": -1669.0,
"itemCommodity": "",
"itemIssueUnit": "LEN",
"itemStructure": false,
"itemDtPole": true,
"requestQuantity": "10.00",
"approvedQuantity": "8.00",
"issuedQuantity": "5.00",
"timestamp": "2022-07-20T11:21:37.607303+08:00",
"updated": "2022-07-21T11:19:39.730245+08:00"
}
]
}
]
}
然后這是我上面 JSON 的 Class :
class MROParent {
int? id;
String? parentWo;
String? parentWoDesc;
String? parentWoStatus;
DateTime? parentWoStatusDate;
String? parentWoWorkType;
String? parentWoClass;
String? parentWoCrew;
String? parentWoLead;
String? parentWoLocation;
String? parentWoDepartmentDescription;
String? parentWoSectionDescription;
String? status;
String? approvedBy;
DateTime? approvedDate;
String? disapprovedBy;
DateTime? disapprovedDate;
String? issuedBy;
DateTime? issuedDate;
String? voidedBy;
DateTime? voidedDate;
String? receivedBy;
DateTime? receivedDate;
List<MROChild>? children;
MROParent({
this.id,
this.parentWo,
this.parentWoDesc,
this.parentWoStatus,
this.parentWoStatusDate,
this.parentWoWorkType,
this.parentWoClass,
this.parentWoCrew,
this.parentWoLead,
this.parentWoLocation,
this.parentWoDepartmentDescription,
this.parentWoSectionDescription,
this.status,
this.approvedBy,
this.approvedDate,
this.disapprovedBy,
this.disapprovedDate,
this.issuedBy,
this.issuedDate,
this.voidedBy,
this.voidedDate,
this.receivedBy,
this.receivedDate,
this.children,
});
factory MROParent.fromJson(Map<String, dynamic> json) {
var list = json['children'] as List;
print(list.runtimeType);
List<MROChild> childrenlist = list.map((i) => MROChild.fromJson(i)).toList();
return MROParent(
id: json["id"],
parentWo: json["parentWo"],
parentWoDesc: json["parentWoDesc"],
parentWoStatus: json["parentWoStatus"],
parentWoStatusDate: DateTime.parse(json["parentWoStatusDate"]),
parentWoWorkType: json["parentWoWorkType"],
parentWoClass: json["parentWoClass"],
parentWoCrew: json["parentWoCrew"],
parentWoLead: json["parentWoLead"],
parentWoLocation: json["parentWoLocation"],
parentWoDepartmentDescription: json["parentWoDepartmentDescription"],
parentWoSectionDescription: json["parentWoSectionDescription"],
status: json["status"],
approvedBy: json["approvedBy"],
approvedDate: DateTime.parse(json["approvedDate"]),
disapprovedBy: json["disapprovedBy"],
disapprovedDate: DateTime.parse(json["disapprovedDate"]),
issuedBy: json["issuedBy"],
issuedDate: DateTime.parse(json["issuedDate"]),
voidedBy: json["voidedBy"],
voidedDate: DateTime.parse(json["voidedDate"]),
receivedBy: json["receivedBy"],
receivedDate: DateTime.parse(json["receivedDate"]),
children:
childrenlist,
);
}
Map<String, dynamic> toJson() => {
"id": id,
"parentWo": parentWo,
"parentWoDesc": parentWoDesc,
"parentWoStatus": parentWoStatus,
"parentWoStatusDate": parentWoStatusDate!.toIso8601String(),
"parentWoWorkType": parentWoWorkType,
"parentWoClass": parentWoClass,
"parentWoCrew": parentWoCrew,
"parentWoLead": parentWoLead,
"parentWoLocation": parentWoLocation,
"parentWoDepartmentDescription": parentWoDepartmentDescription,
"parentWoSectionDescription": parentWoSectionDescription,
"status": status,
"approvedBy": approvedBy,
"approvedDate": approvedDate!.toIso8601String(),
"disapprovedBy": disapprovedBy,
"disapprovedDate": disapprovedDate!.toIso8601String(),
"issuedBy": issuedBy,
"issuedDate": issuedDate!.toIso8601String(),
"voidedBy": voidedBy,
"voidedDate": voidedDate!.toIso8601String(),
"receivedBy": receivedBy,
"receivedDate": receivedDate!.toIso8601String(),
"children": List<dynamic>.from(children!.map((x) => x.toJson())),
};
}
class MROChild {
int? id;
String? woNum;
String? woDesc;
String? woStatus;
DateTime? woStatusDate;
String? woCrew;
String? woLead;
String? woLocation;
List<MROMaterial>? materials;
MROChild({
this.id,
this.woNum,
this.woDesc,
this.woStatus,
this.woStatusDate,
this.woCrew,
this.woLead,
this.woLocation,
this.materials,
});
factory MROChild.fromJson(Map<String, dynamic> json) {
var list = json['materials'] as List;
print(list.runtimeType);
List<MROMaterial> materialslist = list.map((i) => MROMaterial.fromJson(i)).toList();
return MROChild(
id: json["id"],
woNum: json["woNum"],
woDesc: json["woDesc"],
woStatus: json["woStatus"],
woStatusDate: DateTime.parse(json["woStatusDate"]),
woCrew: json["woCrew"],
woLead: json["woLead"],
woLocation: json["woLocation"],
materials: materialslist,
);
}
Map<String, dynamic> toJson() => {
"id": id,
"woNum": woNum,
"woDesc": woDesc,
"woStatus": woStatus,
"woStatusDate": woStatusDate!.toIso8601String(),
"woCrew": woCrew,
"woLead": woLead,
"woLocation": woLocation,
"materials": List<dynamic>.from(materials!.map((x) => x.toJson())),
};
get length => null;
}
class MROMaterial {
int? id;
String? itemNum;
String? itemDescription;
double? itemQuantityPlan;
double? itemQuantityIssued;
double? itemWoBalance;
double? itemInventoryBalance;
String? itemCommodity;
String? itemIssueUnit;
bool? itemStructure;
bool? itemDtPole;
double? requestQuantity;
double? approvedQuantity;
double? issuedQuantity;
DateTime? timestamp;
DateTime? updated;
MROMaterial({
this.id,
this.itemNum,
this.itemDescription,
this.itemQuantityPlan,
this.itemQuantityIssued,
this.itemWoBalance,
this.itemInventoryBalance,
this.itemCommodity,
this.itemIssueUnit,
this.itemStructure,
this.itemDtPole,
this.requestQuantity,
this.approvedQuantity,
this.issuedQuantity,
this.timestamp,
this.updated,
});
factory MROMaterial.fromJson(Map<String, dynamic> json) => MROMaterial(
id: json["id"],
itemNum: json["itemNum"],
itemDescription: json["itemDescription"],
itemQuantityPlan: json["itemQuantityPlan"],
itemQuantityIssued: json["itemQuantityIssued"],
itemWoBalance: json["itemWoBalance"],
itemInventoryBalance: json["itemInventoryBalance"],
itemCommodity: json["itemCommodity"],
itemIssueUnit: json["itemIssueUnit"],
itemStructure: json["itemStructure"],
itemDtPole: json["itemDtPole"],
requestQuantity: json["requestQuantity"],
approvedQuantity: json["approvedQuantity"],
issuedQuantity: json["issuedQuantity"],
timestamp: DateTime.parse(json["timestamp"]),
updated: DateTime.parse(json["updated"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"itemNum": itemNum,
"itemDescription": itemDescription,
"itemQuantityPlan": itemQuantityPlan,
"itemQuantityIssued": itemQuantityIssued,
"itemWoBalance": itemWoBalance,
"itemInventoryBalance": itemInventoryBalance,
"itemCommodity": itemCommodity,
"itemIssueUnit": itemIssueUnit,
"itemStructure": itemStructure,
"itemDtPole": itemDtPole,
"requestQuantity": requestQuantity,
"approvedQuantity": approvedQuantity,
"issuedQuantity": issuedQuantity,
"timestamp": timestamp!.toIso8601String(),
"updated": updated!.toIso8601String(),
};
@override
String toString() {
// TODO: implement toString
return '$id';
}
}
當我嘗試將其保存為新的 class 並嘗試從中打印一個值時,下面的代碼似乎不起作用:
print('THIS HAVE RESULTS IN MRO PARENT'); //this one prints
final parentMROResponse = jsonResponse['results']['data'];
print(parentMROResponse); //this one prints
mainParentMROO = MROParent.fromJson(parentMROResponse);
print(mainParentMROO.id); //THIS IS DOESN'T PRINT
有什么我想念的嗎? 我嘗試檢查數據類型,我認為一切都是正確的。 謝謝!
將int
更改為dynamic
您的問題將得到解決!
class MROMaterial {
dynamic id;
String? itemNum;
String? itemDescription;
dynamic itemQuantityPlan;
dynamic itemQuantityIssued;
dynamic itemWoBalance;
dynamic itemInventoryBalance;
String? itemCommodity;
String? itemIssueUnit;
bool? itemStructure;
bool? itemDtPole;
dynamic requestQuantity;
dynamic approvedQuantity;
dynamic issuedQuantity;
DateTime? timestamp;
DateTime? updated;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.