Flutter开发之——json序列化(1)

List datas;

ArticleData(this.curPage, this.offset, this.over, this.pageCount, this.size, this.total, this.datas);

factory ArticleData.fromJson(Map<String, dynamic> json) => _$ArticleDataFromJson(json);

Map<String, dynamic> toJson() => _$ArticleDataToJson(this);

}

@JsonSerializable()

class Person {

String apkLink;

int audit;

String author;

bool canEdit;

int chapterId;

String chapterName;

bool collect;

int courseId;

String desc;

String descMd;

String envelopePic;

bool fresh;

String host;

int id;

String link;

String niceDate;

String niceShareDate;

String origin;

String prefix;

String projectLink;

int publishTime;

int realSuperChapterId;

int selfVisible;

int shareDate;

String shareUser;

int superChapterId;

String superChapterName;

List tags;

String title;

int type;

int userId;

int visible;

int zan;

Person(

this.apkLink,

this.audit,

this.author,

this.canEdit,

this.chapterId,

this.chapterName,

this.collect,

this.courseId,

this.desc,

this.descMd,

this.envelopePic,

this.fresh,

this.host,

this.id,

this.link,

this.niceDate,

this.niceShareDate,

this.origin,

this.prefix,

this.projectLink,

this.publishTime,

this.realSuperChapterId,

this.selfVisible,

this.shareDate,

this.shareUser,

this.superChapterId,

this.superChapterName,

this.tags,

this.title,

this.type,

this.userId,

this.visible,

this.zan);

factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);

Map<String, dynamic> toJson() => _$PersonToJson(this);

}

@JsonSerializable()

class Tag {

String name;

String url;

Tag(this.name, this.url);

factory Tag.fromJson(Map<String, dynamic> json) => _$TagFromJson(json);

Map<String, dynamic> toJson() => _$TagToJson(this);

}

注意:

  • 添加part 'Article.g.dart';其中:Article是dart文件名,文件名+g.dart格式

  • 在每个需要序列化的类前面添加@JsonSerializable(),如类:ArticleArticleDataPersonTag

  • 每个类里面有:变量,构造函数,类名.fromJson() => _$类名FromJson(json)toJson() => _$类名ToJson(this)

3.3 生成序列化类.g.dart文件

执行如下指令

flutter pub run build_runner build

如无错误发生,生成Article.g.dart文件,Article.dart文件中_$类名FromJson(json)等不会再出现错误

四 序列化后类的应用


4.1 代码

late Future

futureArticle;

@override

void initState() {

super.initState();

futureArticle=_httpGet2();

}

Future

_httpGet2() async {

var httpClient = new HttpClient();

var uri = Uri(

scheme: ‘https’,

host: ‘www.wanandroid.com’,

path: ‘article/list/0/json’,

);

HttpClientRequest request = await httpClient.getUrl(uri);

HttpClientResponse response = await request.close();

String responseBody = await response.transform(utf8.decoder).join();

var jsonDecode = json.decode(responseBody);

var article =Article.fromJson(jsonDecode);

print(article);

return article;

}

//显示

FutureBuilder

(

future: futureArticle,

builder: (context, snapshot) {

if (snapshot.hasData) {

return ListView.separated(

itemCount: snapshot.data!.data.datas.length,

separatorBuilder: (context, index) {return Divider(height: 10, color: Colors.grey,);},

itemBuilder: (BuildContext context, int index) {

return Column(

children: [

Text(“${snapshot.data!.data.datas[index].author}”),

Text(“${snapshot.data!.data.datas[index].title}”),

Text(“${snapshot.data!.data.datas[index].niceShareDate}”)

],

最后

希望本文对你有所启发,有任何面试上的建议也欢迎留言分享给大家。

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

好了~如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
片转存中…(img-L7Y3B4uj-1714740200437)]

好了~如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

[外链图片转存中…(img-mEz9ZYps-1714740200439)]

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值