0
点赞
收藏
分享

微信扫一扫

Flutter开发之——网络请求-手动json数据解析(1),framework框架

钟罗敏 2022-01-31 阅读 56

print(responseBody);

三 网络请求结果json手动解析


3.1 处理前(String类型)

数据类型(String类型)

数据结构

数据结构分层

  • 第一层:errorMsg(String),errorCode(int),data(数据类)

  • 第二层(data):curPage(int),offset(int),over(bool),pageCount(int),size(int),datas(数据集合类)

  • 第三层(data/datas):apkLink(String),audit(int),author(String),canEdit(bool),chapterId(int),chapterName(String),collect(bool),courseId(int),desc(String),descMd(String),envelopePic(String),fresh(bool),host(String),id(int),link(String),niceDate(String),niceShareDate(String),origin(String),prefix(String),projectLink(String),publishTime(int),realSuperChapterId(int),selfVisible(int),shareDate(int),shareUser(String),superChapterId(int),superChapterName(String),tags(数据集合类),title(String),type(int),userId(int),visible(int),zan(int)

  • 第4层(data/datas/t
    ags)

3.2 将String类型转换为Json类型(Map)

调用函数(String类型—>Json)

var jsonDecode = json.decode(responseBody);

3.3 Json类型转换为数据类(Article-第一层)

数据类(data设置为Object)

class Article {

int errorCode;

String errorMsg;

Object data;

Article(this.errorCode, this.errorMsg, this.data);

Article.formJson(Map<String, dynamic> json) {

Article(

errorCode= json[‘errorCode’],

errorMsg = json[‘errorMsg’],

data = json[‘data’]

);

}

}

转换后

3.4 将Json彻底转换为数据类

数据类

class Article {

int errorCode;

String errorMsg;

ArticleData data;

Article(this.errorCode, this.errorMsg, this.data);

Article.formJson(Map<String, dynamic> json) {

Article(

errorCode= json[‘errorCode’],

errorMsg = json[‘errorMsg’],

data = ArticleData.fromJson(json[‘data’])

);

}

}

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);

ArticleData.fromJson(Map<String, dynamic> json) {

var personList = List();

for (Map<String, dynamic> map in json[‘datas’]) {

personList.add(Person.fromJson(map));

}

ArticleData(

curPage = json[‘curPage’],

offset = json[‘curPage’],

over = json[‘over’],

pageCount = json[‘pageCount’],

size = json[‘size’],

total = json[‘total’],

datas = personList);

}

}

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);

Person.fromJson(Map<String, dynamic> json) {

var tagList = List();

for (Map<String, dynamic> map in json[‘tags’]) {

tagList.add(Tag.fromJson(map));

}

Person(

apkLink = json[‘apkLink’],

audit = json[‘audit’],

author = json[‘author’],

canEdit = json[‘canEdit’],

chapterId = json[‘chapterId’],

chapterName = json[‘chapterName’],

collect = json[‘collect’],

courseId = json[‘courseId’],

desc = json[‘desc’],

descMd = json[‘descMd’],

envelopePic = json[‘envelopePic’],

fresh = json[‘fresh’],

host = json[‘host’],

id = json[‘id’],

link = json[‘link’],

niceDate = json[‘niceDate’],

niceShareDate = json[‘niceShareDate’],

origin = json[‘origin’],

prefix = json[‘prefix’],

projectLink = json[‘projectLink’],

publishTime = json[‘publishTime’],

realSuperChapterId = json[‘realSuperChapterId’],

selfVisible = json[‘selfVisible’],

shareDate = json[‘shareDate’],

shareUser = json[‘shareUser’],

superChapterId = json[‘superChapterId’],

superChapterName = json[‘superChapterName’],

tags = tagList,

title = json[‘title’],

type = json[‘type’],

userId = json[‘userId’],

visible = json[‘visible’],

zan = json[‘zan’]);

}

}

class Tag {

String name;

String url;

Tag(this.name, this.url);

erId’],

selfVisible = json[‘selfVisible’],

shareDate = json[‘shareDate’],

shareUser = json[‘shareUser’],

superChapterId = json[‘superChapterId’],

superChapterName = json[‘superChapterName’],

tags = tagList,

title = json[‘title’],

type = json[‘type’],

userId = json[‘userId’],

visible = json[‘visible’],

zan = json[‘zan’]);

}

}

class Tag {

String name;

String url;

Tag(this.name, this.url);

举报

相关推荐

0 条评论