zoukankan      html  css  js  c++  java
  • flutter 复杂数据模型 转换

    json 格式网址:http://www.bejson.com

    json转dart工具网址:https://javiercbk.github.io/json_to_dart/

    代码:

    class CategoryItemModel {
    String mallCategoryId;
    String mallCategoryName;
    List<BxMallSubDto> bxMallSubDto;
    Null comments;
    String image;
    //构造函数
    CategoryItemModel({
    this.mallCategoryId,
    this.mallCategoryName,
    this.bxMallSubDto,
    this.comments,
    this.image
    });

    CategoryItemModel.fromJson(Map<String,dynamic>json){
    mallCategoryId = json['mallCategoryId'];
    mallCategoryName = json['mallCategoryName'];
    if (json['bxMallSubDto'] != null) {
    bxMallSubDto = new List<BxMallSubDto>();
    json['bxMallSubDto'].forEach((i){
    bxMallSubDto.add(new BxMallSubDto.fromJson(i));
    });
    }
     
    comments = json['comments'];
    image = json['image'];
    }
    // factory CategoryItemModel.fromJson(dynamic json){
    // return CategoryItemModel(
    // mallCategoryId:json['mallCategoryId'],
    // mallCategoryName:json['mallCategoryName'],
    // bxMallSubDto:json['bxMallSubDto'],
    // comments:json['comments'],
    // image:json['image']

    // );
    // }
    }

    class CategoryListModel {
    List<CategoryItemModel> data;
    CategoryListModel(this.data);

    CategoryListModel.fromJson(Map<String,dynamic> json){
    if (json['data'] != null) {
    data = new List<CategoryItemModel>();
    json['data'].forEach((v){
    data.add(new CategoryItemModel.fromJson(v));
    });
     
    }
    }

    // factory CategoryListModel.fromJson(List json){
    // return CategoryListModel(
    // json.map((i)=>CategoryItemModel.fromJson((i))).toList()
    // );
    // }
    }

    class BxMallSubDto {
    String mallSubId;
    String mallCategoryId;
    String mallSubName;
    String comments;

    BxMallSubDto(
    {this.mallSubId, this.mallCategoryId, this.mallSubName, this.comments});

    BxMallSubDto.fromJson(Map<String, dynamic> json) {
    mallSubId = json['mallSubId'];
    mallCategoryId = json['mallCategoryId'];
    mallSubName = json['mallSubName'];
    comments = json['comments'];
    }

    // Map<String, dynamic> toJson() {
    // final Map<String, dynamic> data = new Map<String, dynamic>();
    // data['mallSubId'] = this.mallSubId;
    // data['mallCategoryId'] = this.mallCategoryId;
    // data['mallSubName'] = this.mallSubName;
    // data['comments'] = this.comments;
    // return data;
    // }
    }
    使用代码:
    var data = json.decode(val);
    CategoryListModel categoryListModel = CategoryListModel.fromJson(data);
    categoryListModel.data.forEach((item)=>print(item.mallCategoryName));
  • 相关阅读:
    利用栈实现字符串中三种括号的匹配问题c++语言实现
    十进制数转N进制c++实现
    字符单链表识别数字,字母,其它字符,并分为三个循环链表的算法c++实现
    c++两数组合并算法
    c++顺序表(数组)查找最大最小值
    SSO单点登录、跨域重定向、跨域设置Cookie、京东单点登录实例分析
    php实现单点登录,顶级域名与子域名间共享Cookie实现单点登录原理
    php实现单点登录实例
    php实现SSO单点登录实例
    玩转音频、视频的利器:FFmpeg
  • 原文地址:https://www.cnblogs.com/pp-pping/p/12238001.html
Copyright © 2011-2022 走看看