我的第一个建议是使用数字而不是字符串来转换这些数据,因此无论将来标签(ones、twos 等)是否发生变化,都不会影响您的代码:
void main() {
final data = {
"success": true,
"data": {
"ones": [
{ "id": "2", "username": "LM10002" },
{ "id": "6", "username": "LM10006" }
],
"twos": [
{ "id": "3", "username": "LM10003" },
{ "id": "8", "username": "LM10008" }
]
}
};
print(ApiResponse.fromJson(data));
}
class ApiResponse {
final bool success;
final List<List<Data>> data;
ApiResponse({this.success, this.data});
factory ApiResponse.fromJson(Map<String, dynamic> json) {
List<List<Data>> dataList = [];
final jsonData = (json["data"] as Map<String, dynamic>);
jsonData.values.forEach((value) {
List<Data> parsedList = List<Data>.from(List.from(value).map((element) => Data.fromJson(element)));
dataList.add(parsedList);
});
return ApiResponse(
success: json["success"],
data: dataList,
);
}
@override
String toString() {
return "Success: $success, Data: ${data.map((element) => element.map((d) => "${d.id} ${d.username}")).toList().join("; ")}";
}
}
class Data {
final String id;
final String username;
Data({this.id, this.username});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
id: json["id"],
username: json["username"],
);
}
}
这应该返回一个数据列表,其中每个index+1 将代表顺序(一个或两个等...),附加的值将直接是数据列表。
主函数中的打印语句将打印这个:Success: true, Data: (2 LM10002, 6 LM10006); (3 LM10003, 8 LM10008)。
第二种方法是为您的每个数据添加一个group 属性,以便您知道每个数据属于哪个组(ones、twos 等...)
这是完整的实现:
void main() {
final data = {
"success": true,
"data": {
"ones": [
{ "id": "2", "username": "LM10002" },
{ "id": "6", "username": "LM10006" }
],
"twos": [
{ "id": "3", "username": "LM10003" },
{ "id": "8", "username": "LM10008" }
]
}
};
print(ApiResponse.fromJson(data));
}
class ApiResponse {
final bool success;
final List<Data> data;
ApiResponse({this.success, this.data});
factory ApiResponse.fromJson(Map<String, dynamic> json) {
List<Data> dataList = [];
final jsonData = (json["data"] as Map<String, dynamic>);
jsonData.forEach((key, value) {
List<Data> parsedList = List<Data>.from(List.from(value).map((element) {
element["group"] = key;
return Data.fromJson(element);
}));
dataList.addAll(parsedList);
});
return ApiResponse(
success: json["success"],
data: dataList,
);
}
@override
String toString() {
return "Success: $success, Data: ${data.map((element) => "${element.id}, ${element.username}, ${element.group}")}";
}
}
class Data {
final String id;
final String username;
final String group;
Data({this.id, this.username, this.group});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
id: json["id"],
username: json["username"],
group: json["group"]
);
}
}
main 方法中的 print 语句应该打印:Success: true, Data: (2, LM10002, ones, 6, LM10006, ones, 3, LM10003, twos, 8, LM10008, twos)
总结一下:
| Solution |
Description |
Advantage |
Disadvantage |
| #1 |
Proposes to represent the data in the form of a matrix with in ordinates the indexes-1 of the "groups" of data, and in abcissa, the data themselves |
We keep the order of the data and it is easy to sort them |
Not great for readability, and the group labels returned by the API are |
| #2 |
Allows to keep the labels as strings and add them to the data (to later filter the data automatically according to the groups) |
We keep the initial data group labels |
It will be more difficult to perform sorting |