老师,我反序列化带数组的对象有问题。

来源:9-4 JSON解析与复杂模型转换实用技巧【网络请求离不开它】

东方蓂荚

2019-09-24

老师,你好。我这样反序列化一个包含数组的对象 报错,报homeMenu为空,麻烦您看一下,谢谢。
Future fetchData() async {
final response =
await http.get(‘http://daoye365.com:8081/api/homemodel/GetHomeFunctions’);

//防止乱码
Map<String,dynamic> result = json.decode(Utf8Decoder().convert(response.bodyBytes));

HomeModel homeModel =HomeModel.fromJson(result);

return homeModel;
}

class HomeModel {
List homeMenu;

HomeModel({this.homeMenu});

HomeModel.fromJson(Map<String, dynamic> json) {
if (json[‘HomeMenu’] != null) {
homeMenu = new List();
json[‘HomeMenu’].forEach((v) {
homeMenu.add(new HomeMenu.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.homeMenu != null) {
data[‘HomeMenu’] = this.homeMenu.map((v) => v.toJson()).toList();
}
return data;
}
}

class HomeMenu {
String menuID;
String groupConstantID;
String title;
String url;
int sortNo;
int isShow;
int isLimit;
String icon;
Null creator;
String createTime;
Null modifier;
String modifyTime;
int deleteFlag;
String isShowName;
String isLimitName;
bool isOpenPage;

HomeMenu(
{this.menuID,
this.groupConstantID,
this.title,
this.url,
this.sortNo,
this.isShow,
this.isLimit,
this.icon,
this.creator,
this.createTime,
this.modifier,
this.modifyTime,
this.deleteFlag,
this.isShowName,
this.isLimitName,
this.isOpenPage});

HomeMenu.fromJson(Map<String, dynamic> json) {
menuID = json[‘MenuID’];
groupConstantID = json[‘GroupConstantID’];
title = json[‘Title’];
url = json[‘Url’];
sortNo = json[‘SortNo’];
isShow = json[‘IsShow’];
isLimit = json[‘IsLimit’];
icon = json[‘Icon’];
creator = json[‘Creator’];
createTime = json[‘CreateTime’];
modifier = json[‘Modifier’];
modifyTime = json[‘ModifyTime’];
deleteFlag = json[‘DeleteFlag’];
isShowName = json[‘IsShowName’];
isLimitName = json[‘IsLimitName’];
isOpenPage = json[‘IsOpenPage’];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data[‘MenuID’] = this.menuID;
data[‘GroupConstantID’] = this.groupConstantID;
data[‘Title’] = this.title;
data[‘Url’] = this.url;
data[‘SortNo’] = this.sortNo;
data[‘IsShow’] = this.isShow;
data[‘IsLimit’] = this.isLimit;
data[‘Icon’] = this.icon;
data[‘Creator’] = this.creator;
data[‘CreateTime’] = this.createTime;
data[‘Modifier’] = this.modifier;
data[‘ModifyTime’] = this.modifyTime;
data[‘DeleteFlag’] = this.deleteFlag;
data[‘IsShowName’] = this.isShowName;
data[‘IsLimitName’] = this.isLimitName;
data[‘IsOpenPage’] = this.isOpenPage;
return data;
}

写回答

1回答

CrazyCodeBoy

2019-09-25

接口布置的有问题,这个接口应当返回json类型的数据,实际返回的确是xml类型的数据

0
1
东方蓂荚
非常感谢!
2019-09-25
共1条回复

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3274 问题

查看课程