Problem Description:

A complex Map object with a jsonArray format is returned by the backend of the flutter when the network data is parsed.

"data":{ "1908":[ { "id":"1099", "createDate":"2021-05-31 00:41:12", "modifyDate":"2021-06-01 10:57:19", "orders":0, "beginDate":"2021-05-31 00:00:00", "content":"", "endDate":"2031-05-31 23:59:59", "ImgPath" : "https://xxx/upload/image/20210601/qpN5dnoh3.png", "title" : "a single AD 1", "adPosition" : "1908", The first banner "adPositionName" : ""}]," 1909 ": [{" id" : "1157", "createDate" : "the 2021-06-03 16:38:18", "modifyDate":"2021-07-14 13:54:31", "orders":0, "beginDate":"2021-07-16 00:00:00", "content":"", "EndDate", "23:59:59" 2021-07-16, "imgPath" : "https://xxx/upload/image/20210714/Mp35LWiP3.jpg", "title" : "round 1" ads, "AdPosition ":"1909", "adPositionName":" second banner", {"id":"1211", "createDate":"2021-06-24 08:50:38", "modifyDate":"2021-07-14 13:53:03", "orders":0, "beginDate":"2021-07-16 00:00:00", "content":"", "EndDate", "the 2021-07-16 23:59:59", "imgPath" : "HTTPS: / XXX/upload/images / 20210714 / qpGiCkV - 3. PNG", "title" : "by advertising 2", "AdPosition ":"1909", "adPositionName":" second banner"}]}Copy the code

Solution:

The data format is Map<String,List>mapData. Mapdata. forEach((key,value){//key is 1908,value is List data. })

Without further ado, let’s get right to the code

@JsonSerializable()
class FindAdByIdResponse extends BaseJsonResponse {
  Map<String, List<FindAdByIdData>> data;

  FindAdByIdResponse(int code, String message, DateTime serverTime, this.data) : super(code, message, serverTime);

  factory FindAdByIdResponse.fromJson(Map<String, dynamic> json) => _$FindAdByIdResponseFromJson(json);

  Map<String, dynamic> toJson() => _$FindAdByIdResponseToJson(this);
}
Copy the code
FindAdByIdResponse response = FindAdByIdResponse.fromJson(data.data); Map<String, List<FindAdByIdData>>mapData = response.data; Mapdata. forEach((key, value) {//key is 1908, value is List<FindAdByIdData>});Copy the code

summary

If this article is helpful, please give it a thumbs up