记一次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"],
      );
}
posted @   老卫同学  阅读(280)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示
主题色彩