将一个中等复杂度的json转成model
1.7k
等61人参与

描述:
将下面json转换成model

{
  "code": 0,
  "data": {
    "total": 6,
    "list": [
      {
        "id": "9",
        "sticky": 1,
        "type": "recommend",
        "title": "好课推荐",
        "subtitle": "移动端普通工程师到架构师的全方位蜕变",
        "url": "http://class.imooc.com/sale/mobilearchitect",
        "cover": "https://img1.sycdn.imooc.com//5f057a6a0001f4f918720764.jpg",
        "createTime": "2020-12-03 16:39:24"
      }
    ]
  },
  "msg": "SUCCESS."
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
代码块
复制 预览
复制成功!
代码块
复制 预览
复制成功!

思路:

可以手写model也可以借助https://www.devio.org/io/tools/json-to-dart/ 来实现model的在线转换

我的作业
去发布

登录后即可发布作业,立即

全部作业

class Autogenerated {
int? code;
Data? data;
String? msg;

Autogenerated({this.code, this.data, this.msg});

Autogenerated.fromJson(Map<String, dynamic> json) {
code = json[‘code’];
data = json[‘data’] != null ? new Data.fromJson(json[‘data’]) : null;
msg = json[‘msg’];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data[‘code’] = this.code;
if (this.data != null) {
data[‘data’] = this.data!.toJson();
}
data[‘msg’] = this.msg;
return data;
}
}

class Data {
int? total;
List? list;

Data({this.total, this.list});

Data.fromJson(Map<String, dynamic> json) {
total = json[‘total’];
if (json[‘list’] != null) {
list = [];
json[‘list’].forEach((v) {
list!.add(new List.fromJson(v));
});
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data[‘total’] = this.total;
if (this.list != null) {
data[‘list’] = this.list!.map((v) => v.toJson()).toList();
}
return data;
}
}

class List {
String? id;
int? sticky;
String? type;
String? title;
String? subtitle;
String? url;
String? cover;
String? createTime;

List(
{this.id,
this.sticky,
this.type,
this.title,
this.subtitle,
this.url,
this.cover,
this.createTime});

List.fromJson(Map<String, dynamic> json) {
id = json[‘id’];
sticky = json[‘sticky’];
type = json[‘type’];
title = json[‘title’];
subtitle = json[‘subtitle’];
url = json[‘url’];
cover = json[‘cover’];
createTime = json[‘createTime’];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data[‘id’] = this.id;
data[‘sticky’] = this.sticky;
data[‘type’] = this.type;
data[‘title’] = this.title;
data[‘subtitle’] = this.subtitle;
data[‘url’] = this.url;
data[‘cover’] = this.cover;
data[‘createTime’] = this.createTime;
return data;
}
}

0
评论
提交于  2024-04-14 09:53:11

登录后即可查看更多作业,立即

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号