dependencies:
json_annotation: ^4.0.1 #json_annotation版本
dev_dependencies:
build_runner: ^2.0.3 #build_runner版本
json_serializable: ^4.1.2 #json_serializable版本
3.1 接口文件
- API:https://www.wanandroid.com/article/list/0/json
3.2 序列化类(Article.dart)
import ‘package:json_annotation/json_annotation.dart’;
part ‘Article.g.dart’;
@JsonSerializable()
class Article {
int errorCode;
String errorMsg;
ArticleData data;
Article(this.errorCode, this.errorMsg, this.data);
factory Article.fromJson(Map<String, dynamic> json) => _$ArticleFromJson(json);
Map<String, dynamic> toJson() => _$ArticleToJson(this);
}
@JsonSerializable()
class ArticleData {
int curPage;
int offset;
bool over;
int pageCount;
int size;
int total;
List datas;
ArticleData(this.curPage, this.offset, this.over, this.pageCount, this.size, this.total, this.datas);
factory ArticleData.fromJson(Map<String, dynamic> json) =
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
_$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);
}
注意: