Flutter实战视频-移动电商-22.JSON解析和复杂数据模型转换技巧
22.JSON解析和复杂数据模型转换技巧
json转Model类
创建model文件夹,在里面新建category.dart类
主要根据这个json来分析我们要做成类的样子
{
"code": "",
"message": "success",
"data": [
{
"mallCategoryId": "",
"mallCategoryName": "白酒",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c94621970a801626a35cb4d0175",
"mallCategoryId": "",
"mallSubName": "名酒",
"comments": ""
},
{
"mallSubId": "2c9f6c94621970a801626a363e5a0176",
"mallCategoryId": "",
"mallSubName": "宝丰",
"comments": ""
},
{
"mallSubId": "2c9f6c94621970a801626a3770620177",
"mallCategoryId": "",
"mallSubName": "北京二锅头",
"comments": ""
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cc035c15a8",
"mallCategoryId": "",
"mallSubName": "大明",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cc2af915a9",
"mallCategoryId": "",
"mallSubName": "杜康",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cc535115aa",
"mallCategoryId": "",
"mallSubName": "顿丘",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cc77b215ab",
"mallCategoryId": "",
"mallSubName": "汾酒",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cca72e15ac",
"mallCategoryId": "",
"mallSubName": "枫林",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cccae215ad",
"mallCategoryId": "",
"mallSubName": "高粱酒",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7ccf0d915ae",
"mallCategoryId": "",
"mallSubName": "古井",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cd1d6715af",
"mallCategoryId": "",
"mallSubName": "贵州大曲",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cd3f2815b0",
"mallCategoryId": "",
"mallSubName": "国池",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cd5d3015b1",
"mallCategoryId": "",
"mallSubName": "国窖",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cd7ced15b2",
"mallCategoryId": "",
"mallSubName": "国台",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cd9b9015b3",
"mallCategoryId": "",
"mallSubName": "汉酱",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cdbfd215b4",
"mallCategoryId": "",
"mallSubName": "红星",
"comments": null
},
{
"mallSubId": "2c9f6c946891d16801689474e2a70081",
"mallCategoryId": "",
"mallSubName": "怀庄",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cdddf815b5",
"mallCategoryId": "",
"mallSubName": "剑南春",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cdfd4815b6",
"mallCategoryId": "",
"mallSubName": "江小白",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb1016802277c37160e",
"mallCategoryId": "",
"mallSubName": "金沙",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7ce207015b7",
"mallCategoryId": "",
"mallSubName": "京宫",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7ce46d415b8",
"mallCategoryId": "",
"mallSubName": "酒鬼",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb101680226de23160d",
"mallCategoryId": "",
"mallSubName": "口子窖",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7ce705515b9",
"mallCategoryId": "",
"mallSubName": "郎酒",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7ce989e15ba",
"mallCategoryId": "",
"mallSubName": "老口子",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cec30915bb",
"mallCategoryId": "",
"mallSubName": "龙江家园",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cef15c15bc",
"mallCategoryId": "",
"mallSubName": "泸州",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cf156f15bd",
"mallCategoryId": "",
"mallSubName": "鹿邑大曲",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cf425b15be",
"mallCategoryId": "",
"mallSubName": "毛铺",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cf9dc915c0",
"mallCategoryId": "",
"mallSubName": "绵竹",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cfbf1c15c1",
"mallCategoryId": "",
"mallSubName": "难得糊涂",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cfdd7215c2",
"mallCategoryId": "",
"mallSubName": "牛二爷",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7cf71e715bf",
"mallCategoryId": "",
"mallSubName": "茅台",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7d7eda715c3",
"mallCategoryId": "",
"mallSubName": "绵竹",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7d96e5c15c4",
"mallCategoryId": "",
"mallSubName": "难得糊涂",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dab93b15c5",
"mallCategoryId": "",
"mallSubName": "牛二爷",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dae16415c6",
"mallCategoryId": "",
"mallSubName": "牛栏山",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7db11cb15c7",
"mallCategoryId": "",
"mallSubName": "前门",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7db430c15c8",
"mallCategoryId": "",
"mallSubName": "全兴",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7db6cac15c9",
"mallCategoryId": "",
"mallSubName": "舍得",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7db9a4415ca",
"mallCategoryId": "",
"mallSubName": "双沟",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dc30b815cb",
"mallCategoryId": "",
"mallSubName": "水井坊",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dc543e15cc",
"mallCategoryId": "",
"mallSubName": "四特",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dc765c15cd",
"mallCategoryId": "",
"mallSubName": "潭酒",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dc988a15ce",
"mallCategoryId": "",
"mallSubName": "沱牌",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dcba5a15cf",
"mallCategoryId": "",
"mallSubName": "五粮液",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dcd9e815d0",
"mallCategoryId": "",
"mallSubName": "西凤",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dcf6d715d1",
"mallCategoryId": "",
"mallSubName": "习酒",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dd11b215d2",
"mallCategoryId": "",
"mallSubName": "小白杨",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dd2f3c15d3",
"mallCategoryId": "",
"mallSubName": "洋河",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7dd969115d4",
"mallCategoryId": "",
"mallSubName": "伊力特",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7ddb16c15d5",
"mallCategoryId": "",
"mallSubName": "张弓",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7ddd6c715d6",
"mallCategoryId": "",
"mallSubName": "中粮",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7de126815d7",
"mallCategoryId": "",
"mallSubName": "竹叶青",
"comments": null
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170036_4477.png"
},
{
"mallCategoryId": "",
"mallCategoryName": "啤酒",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c946016ea9b016016f79c8e0000",
"mallCategoryId": "",
"mallSubName": "百威",
"comments": ""
},
{
"mallSubId": "2c9f6c94608ff843016095163b8c0177",
"mallCategoryId": "",
"mallSubName": "福佳",
"comments": ""
},
{
"mallSubId": "402880e86016d1b5016016db9b290001",
"mallCategoryId": "",
"mallSubName": "哈尔滨",
"comments": ""
},
{
"mallSubId": "402880e86016d1b5016016dbff2f0002",
"mallCategoryId": "",
"mallSubName": "汉德",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647cd6830e0022",
"mallCategoryId": "",
"mallSubName": "崂山",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647cd706a60023",
"mallCategoryId": "",
"mallSubName": "林德曼",
"comments": ""
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e1411b15d8",
"mallCategoryId": "",
"mallSubName": "青岛",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e1647215d9",
"mallCategoryId": "",
"mallSubName": "三得利",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e182e715da",
"mallCategoryId": "",
"mallSubName": "乌苏",
"comments": null
},
{
"mallSubId": "2c9f6c9468118c9c016811ab16bf0001",
"mallCategoryId": "",
"mallSubName": "雪花",
"comments": null
},
{
"mallSubId": "2c9f6c9468118c9c016811aa6f440000",
"mallCategoryId": "",
"mallSubName": "燕京",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e19b8f15db",
"mallCategoryId": "",
"mallSubName": "智美",
"comments": null
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170044_9165.png"
},
{
"mallCategoryId": "",
"mallCategoryName": "葡萄酒",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c9460337d540160337fefd60000",
"mallCategoryId": "",
"mallSubName": "澳大利亚",
"comments": ""
},
{
"mallSubId": "402880e86016d1b5016016e083f10010",
"mallCategoryId": "",
"mallSubName": "德国",
"comments": ""
},
{
"mallSubId": "402880e86016d1b5016016df1f92000c",
"mallCategoryId": "",
"mallSubName": "法国",
"comments": ""
},
{
"mallSubId": "2c9f6c94621970a801626a40feac0178",
"mallCategoryId": "",
"mallSubName": "南非",
"comments": ""
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e5c9a115dc",
"mallCategoryId": "",
"mallSubName": "葡萄牙",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e5e68f15dd",
"mallCategoryId": "",
"mallSubName": "西班牙",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e609f515de",
"mallCategoryId": "",
"mallSubName": "新西兰",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e6286a15df",
"mallCategoryId": "",
"mallSubName": "意大利",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e6486615e0",
"mallCategoryId": "",
"mallSubName": "智利",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7e66c6815e1",
"mallCategoryId": "",
"mallSubName": "中国",
"comments": null
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170053_878.png"
},
{
"mallCategoryId": "",
"mallCategoryName": "清酒洋酒",
"bxMallSubDto": [
{
"mallSubId": "402880e86016d1b5016016e135440011",
"mallCategoryId": "",
"mallSubName": "清酒",
"comments": ""
},
{
"mallSubId": "402880e86016d1b5016016e171cc0012",
"mallCategoryId": "",
"mallSubName": "洋酒",
"comments": ""
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170101_6957.png"
},
{
"mallCategoryId": "",
"mallCategoryName": "保健酒",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c94609a62be0160a02d1dc20021",
"mallCategoryId": "",
"mallSubName": "黄酒",
"comments": ""
},
{
"mallSubId": "2c9f6c94648837980164883ff6980000",
"mallCategoryId": "",
"mallSubName": "药酒",
"comments": ""
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170110_6581.png"
},
{
"mallCategoryId": "2c9f6c946449ea7e01647ccd76a6001b",
"mallCategoryName": "预调酒",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c946449ea7e01647d02f6250026",
"mallCategoryId": "2c9f6c946449ea7e01647ccd76a6001b",
"mallSubName": "果酒",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647d031bae0027",
"mallCategoryId": "2c9f6c946449ea7e01647ccd76a6001b",
"mallSubName": "鸡尾酒",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647d03428f0028",
"mallCategoryId": "2c9f6c946449ea7e01647ccd76a6001b",
"mallSubName": "米酒",
"comments": ""
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170124_4760.png"
},
{
"mallCategoryId": "2c9f6c946449ea7e01647ccf3b97001d",
"mallCategoryName": "下酒小菜",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c946449ea7e01647dc18e610035",
"mallCategoryId": "2c9f6c946449ea7e01647ccf3b97001d",
"mallSubName": "熟食",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dc1d9070036",
"mallCategoryId": "2c9f6c946449ea7e01647ccf3b97001d",
"mallSubName": "火腿",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dc1fc3e0037",
"mallCategoryId": "2c9f6c946449ea7e01647ccf3b97001d",
"mallSubName": "速冻食品",
"comments": ""
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170133_4419.png"
},
{
"mallCategoryId": "2c9f6c946449ea7e01647ccdb0e0001c",
"mallCategoryName": "饮料",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c946449ea7e01647d09cbf6002d",
"mallCategoryId": "2c9f6c946449ea7e01647ccdb0e0001c",
"mallSubName": "茶饮",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647d09f7e8002e",
"mallCategoryId": "2c9f6c946449ea7e01647ccdb0e0001c",
"mallSubName": "水饮",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647d0a27e1002f",
"mallCategoryId": "2c9f6c946449ea7e01647ccdb0e0001c",
"mallSubName": "功能饮料",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647d0b1d4d0030",
"mallCategoryId": "2c9f6c946449ea7e01647ccdb0e0001c",
"mallSubName": "果汁",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647d14192b0031",
"mallCategoryId": "2c9f6c946449ea7e01647ccdb0e0001c",
"mallSubName": "含乳饮料",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647d24d9600032",
"mallCategoryId": "2c9f6c946449ea7e01647ccdb0e0001c",
"mallSubName": "碳酸饮料",
"comments": ""
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170143_361.png"
},
{
"mallCategoryId": "2c9f6c946449ea7e01647cd108b60020",
"mallCategoryName": "乳制品",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c946449ea7e01647dd4ac8c0048",
"mallCategoryId": "2c9f6c946449ea7e01647cd108b60020",
"mallSubName": "常温纯奶",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dd4f6a40049",
"mallCategoryId": "2c9f6c946449ea7e01647cd108b60020",
"mallSubName": "常温酸奶",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dd51ab7004a",
"mallCategoryId": "2c9f6c946449ea7e01647cd108b60020",
"mallSubName": "低温奶",
"comments": ""
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170151_9234.png"
},
{
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallCategoryName": "休闲零食",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c946449ea7e01647dc51d93003c",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "方便食品",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dd204dc0040",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "面包糕点",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dd22f760041",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "糖果巧克力",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dd254530042",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "膨化食品",
"comments": ""
},
{
"mallSubId": "2c9f6c94679b4fb10167f7fa132b15e7",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "坚果炒货",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7f4bfc415e2",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "肉干豆干",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7f5027a15e3",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "饼干",
"comments": null
},
{
"mallSubId": "2c9f6c94679b4fb10167f7f530fd15e4",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "冲调",
"comments": null
},
{
"mallSubId": "2c9f6c94683a6b0d016846b49436003b",
"mallCategoryId": "2c9f6c946449ea7e01647ccfddb3001e",
"mallSubName": "休闲水果",
"comments": null
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170200_7553.png"
},
{
"mallCategoryId": "2c9f6c946449ea7e01647cd08369001f",
"mallCategoryName": "粮油调味",
"bxMallSubDto": [
{
"mallSubId": "2c9f6c946449ea7e01647dd2e8270043",
"mallCategoryId": "2c9f6c946449ea7e01647cd08369001f",
"mallSubName": "油/粮食",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dd31bca0044",
"mallCategoryId": "2c9f6c946449ea7e01647cd08369001f",
"mallSubName": "调味品",
"comments": ""
},
{
"mallSubId": "2c9f6c946449ea7e01647dd35a980045",
"mallCategoryId": "2c9f6c946449ea7e01647cd08369001f",
"mallSubName": "酱菜罐头",
"comments": ""
}
],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20181212/20181212115842_9733.png"
},
{
"mallCategoryId": "2c9f6c9468a85aef016925444ddb271b",
"mallCategoryName": "积分商品",
"bxMallSubDto": [ ],
"comments": null,
"image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190225/20190225232703_9950.png"
}
]
}
json数据
class CategoryBigModel{
String mallCategoryId;//类别编号
String mallCategoryName;//类别名称
}
comments这个字段都是null值。
所以在创建了的时候 我们直接用null来表示
我们上面只做了一个模型类:CategoryBigModel
在json里面是有多个CategoryBigModel类的。就是一个list的数据。CategoryBigListModel
使用我们定义的model,和model的list集合
我们要输出大类的名称
输出我们的类别
快速生成我们的dart类,数据模型
https://javiercbk.github.io/json_to_dart/
把所有的json复制过来,自动生成类
最终代码
class CategoryBigModel{
String mallCategoryId;//类别编号
String mallCategoryName;//类别名称
List<dynamic> bxMallSubDto;//不知道具体模型就用dynamic类型
Null comments;
String image; //构造方法
CategoryBigModel({
this.mallCategoryId,
this.mallCategoryName,
this.bxMallSubDto,
this.comments,
this.image
});
//工厂类的构造方法,就是我常用的实例化对象模式,好处是我们调用这个类的时候不用再使用new关键字了
//相当于java里面的多态,用起来和我们的静态方法是一样的
factory CategoryBigModel.formJson(dynamic json){
return CategoryBigModel(
mallCategoryId:json['mallCategoryId'],
mallCategoryName:json['mallCategoryName'],
bxMallSubDto:json['bxMallSubDto'],
comments:json['comments'],
image:json['image']
);
}
} //列表的model
class CategoryBigListModel{
List<CategoryBigModel> data;
CategoryBigListModel(this.data); factory CategoryBigListModel.fromJson(List json){
return CategoryBigListModel(
json.map((i)=>CategoryBigModel.formJson((i))).toList()
);
}
}
category.dart
import 'package:flutter/material.dart';
import '../service/service_method.dart';
import 'dart:convert';
import '../model/category.dart'; class CategoryPage extends StatefulWidget {
@override
_CategoryPageState createState() => _CategoryPageState();
} class _CategoryPageState extends State<CategoryPage> {
@override
Widget build(BuildContext context) {
_getCategory();
return Container(
child:Center(
child: Text('分类页面'),
)
);
} void _getCategory() async{
await request('getCategory').then((val){
var data=json.decode(val.toString());
//print(data);
CategoryBigListModel list= CategoryBigListModel.fromJson(data['data']);
list.data.forEach((item)=>print(item.mallCategoryName));
});
}
}
category_page.dart
class CategoryBigModel{
String mallCategoryId;//类别编号
String mallCategoryName;//类别名称
List<dynamic> bxMallSubDto;//不知道具体模型就用dynamic类型
Null comments;
String image; //构造方法
CategoryBigModel({
this.mallCategoryId,
this.mallCategoryName,
this.bxMallSubDto,
this.comments,
this.image
});
//工厂类的构造方法,就是我常用的实例化对象模式,好处是我们调用这个类的时候不用再使用new关键字了
//相当于java里面的多态,用起来和我们的静态方法是一样的
factory CategoryBigModel.formJson(dynamic json){
return CategoryBigModel(
mallCategoryId:json['mallCategoryId'],
mallCategoryName:json['mallCategoryName'],
bxMallSubDto:json['bxMallSubDto'],
comments:json['comments'],
image:json['image']
);
}
} //列表的model
class CategoryBigListModel{
List<CategoryBigModel> data;
CategoryBigListModel(this.data); factory CategoryBigListModel.fromJson(List json){
return CategoryBigListModel(
json.map((i)=>CategoryBigModel.formJson((i))).toList()
);
}
}
Flutter实战视频-移动电商-22.JSON解析和复杂数据模型转换技巧的更多相关文章
- Flutter实战视频-移动电商-17.首页_楼层组件的编写技巧
17.首页_楼层组件的编写技巧 博客地址: https://jspang.com/post/FlutterShop.html#toc-b50 楼层的效果: 标题 stlessW快速生成: 接收一个St ...
- Flutter实战视频-移动电商-02.Flutter实战建立项目和编写入口文件
02.Flutter实战建立项目和编写入口文件 创建项目: flutter create flutter_shop 创建完成之后呢,它会提示我们, 进入flutter_shop的目录,然后执行flut ...
- Flutter移动电商实战 --(22)JSON解析和复杂数据模型转换技巧
json转Model类 创建model文件夹,在里面新建category.dart类 主要根据这个json来分析我们要做成类的样子 { "code": "0", ...
- Flutter实战视频-移动电商-10.首页_FlutterSwiper轮播效果制作
10.首页_FlutterSwiper轮播效果制作 博客地址: https://jspang.com/post/FlutterShop.html#toc-5c2 flutter_swiper http ...
- Flutter实战视频-移动电商-34.列表页_小BUG的修复
34.列表页_小BUG的修复 当高粱酒的子类没有数据返回的时候就会报错. 解决接口空数据报错的问题 没有数据的时候,给用户一个友好的提示, 我们没有数据的时候还要告诉用户,提示一下他没有数据,在我们的 ...
- Flutter实战视频-移动电商-06.Dio基础_Get请求和动态组件协作
博客地址: https://jspang.com/post/FlutterShop.html#toc-0ee 编写页面代码 创建动态组件HomePage,原来的代码是静态的我们这里就去掉就可以了. 然 ...
- Flutter实战视频-移动电商-08.Dio基础_伪造请求头获取数据
08.Dio基础_伪造请求头获取数据 上节课代码清楚 重新编写HomePage这个动态组件 开始写请求的方法 请求数据 .但是由于我们没加请求的头 所以没有返回数据 451就是表示请求错错误 创建请求 ...
- Flutter实战视频-移动电商-09.首页_项目结构建立和获取数据
09.首页_项目结构建立和获取数据 在config下创建service_url.dart 用来配置我们后端接口的配置文件 一个变量存 接口地址,一个接口方法地址 所有后天请求数据的方法都放在这个文件夹 ...
- Flutter实战视频-移动电商-11.首页_屏幕适配方案讲解
11.首页_屏幕适配方案讲解 国人写的屏幕适配插件: https://github.com/OpenFlutter/flutter_screenutil 最新版本是0.5.1 在pubspec.yam ...
随机推荐
- 抽象类的子类能够new
纠结了半天,我以为继承了Activity后不能new这里被那个onCreate方法迷惑了以为会出现故障一直没直接创建对象类使用 后来试了试才知道 activity似乎是一个抽象类吧. 你要用他的方法, ...
- PE添加Style
1. <style id="NumberStyle"> <setting> <param name="option"> ...
- JavaScript事件在WebKit中的处理流程研究
本文主要探讨了JavaScript事件在WebKit中的注冊和触发机制. JS事件有两种注冊方式: 通过DOM节点的属性加入或者通过node.addEventListener()函数注冊: 通过DOM ...
- 五、Web框架基础(2)
Tornado 异步协程编程.(其实是异步IO而非真正的异步,从内核拷贝到用户空间的过程还是同步的) 适合用户量大.高并发,如抢票.网页游戏.在线聊天等场景:或大量HTTP持久连接,通过单TCP持久连 ...
- httpclient4 模拟访问网页 模拟登录 简单例子
JAVA后台模拟登录一个网站,获得一定权限后进一步操作. 所用的工具: Apache HttpComponents client 4.3版本 以下为代码: import org.apache.http ...
- wprintf、wcout无法输出中文的解决方案
在C语言中,若wprintf无法输出中文,调用函数setlocale(int category, const char *locale)设置locale即可输出中文 此方法也可用于C++中 例: #i ...
- bluebird-api简介及demo
var Promise = require("bluebird"); var fs = require("fs"); //方法Promise化 var read ...
- 一起来学linux:PAM模块
在Linux中执行有些程序时,这些程序在执行前首先要对启动它的用户进行认证,符合一定的要求之后才允许执行,例如login, su等 在Linux中进行身份或是状态的验证程序是由PAM来进行的,PAM( ...
- 区块链+AI将给区块链带来怎样的改变?
区块链和人工智能技术都是互联网时代最新.最热的技术,不仅可以改变我们生活,还能产生巨大的财富,为此国家大力支持发展,科技巨头们也纷纷布局.那区块链与人工智能结合,对区块链技术而言会产生什么样的化学反应 ...
- ubuntu12.04离线安装libjpeg62-dev
0:如果的电脑能连接上网络,用apt-get install安装最爽,我的情况是:公司电脑用的内网,访问不了外网,而且不让访问外网,安装软件只能用u盘拷进去再安装,所以我用如下方法 1:下载安装包,地 ...