【发布时间】:2019-09-21 12:28:14
【问题描述】:
我正在使用一个 RESTful API,该 API 似乎在其他应用程序中工作和使用,它给了我这样的东西:
"notes": [
[
{
"automaticNote": false,
"contactId": 0,
"caseFileId": 0,
"dateCreated": "2019-05-02",
"deletedTime": "2019-05-02T19:31:54.588Z"
}
]
]
双方括号意味着一对方括号没有与之关联的名称/键。更糟糕的是,notes 本身嵌套在一些复杂的 JSON 中。
我尝试使用JSON to Dart,但它会引发错误。所以我的问题是,如何序列化没有键/名称的 JSON 数组?
我通常会这样做:
class Note {
bool automaticNote;
int contactId;
int caseFileId;
String dateCreated;
String deletedTime;
Note(
{this.automaticNote,
this.contactId,
this.caseFileId,
this.dateCreated,
this.deletedTime});
Note.fromJson(Map<String, dynamic> json) {
automaticNote = json['automaticNote'];
contactId = json['contactId'];
caseFileId = json['caseFileId'];
dateCreated = json['dateCreated'];
deletedTime = json['deletedTime'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['automaticNote'] = this.automaticNote;
data['contactId'] = this.contactId;
data['caseFileId'] = this.caseFileId;
data['dateCreated'] = this.dateCreated;
data['deletedTime'] = this.deletedTime;
return data;
}
}
但是双 JSON 数组让我感到厌烦(同样,notes 本身嵌套在一个更复杂的 JSON 对象中,但为了简单起见,我没有在此处包含全部内容)。
谢谢!
【问题讨论】:
标签: json parsing serialization dart flutter