其他分享
首页 > 其他分享> > 记一次Flutter Json数组转换为List对象

记一次Flutter Json数组转换为List对象

作者:互联网

在dio请求数据之后进行fromJson操作

// response是请求接口后返回的json数据,调用fromJson方法
DevicePageListResponseEntity.fromJson(response);

// 实体类
class DevicePageListResponseEntity {
  int? code;
  List<DeviceItem>? items;

  DevicePageListResponseEntity({
    this.code,
    this.items,
  });

  factory DevicePageListResponseEntity.fromJson(Map<String, dynamic> json) =>
      DevicePageListResponseEntity(
          code: json["code"],
          items: json["data"]["items"] == null
              ? []
              : List<DeviceChannel>.from(
                  json["data"]["items"].map((x) => DeviceChannel.fromJson(x))));
}

class DeviceChannel {
  int? channelId;
  int? byEnable;
  String? rtspStream;

  DeviceChannel({
    this.channelId,
    this.byEnable,
    this.rtspStream,
  });

  Map<String, dynamic> toJson() => {
        "channelId": channelId,
        "byEnable": byEnable,
        "rtspStream": rtspStream,
      };

  factory DeviceChannel.fromJson(Map<String, dynamic> json) => DeviceChannel(
        channelId: json["channelId"],
        byEnable: json["byEnable"],
        rtspStream: json["rtspStream"],
      );
}

标签:DeviceChannel,channelId,List,json,Json,rtspStream,byEnable,Flutter,fromJson
来源: https://www.cnblogs.com/oldweipro/p/16701357.html