1.简单的json解析类定义
简单的json解析,类里面只有基本数据类型 info.dart
class Info{
int enddate;
String imageurl;
String name;
int id;
String linkurl;
int startdate;
Info({this.enddate,this.imageurl,this.name,this.id,this.linkurl,this.startdate});
factory Info.fromJson(Map<String,dynamic> json){
return Info(enddate:json["enddate"],imageurl:json["imageUrl"],name:json["name"].toString(),id:json["id"],linkurl: json["linkurl"],startdate: json["startdate"]);
}
}
2.带list的json数据解析
launch_info.dart
class LaunchInfo{
String name;
String reportStatus;
List<Info> infos;
LaunchInfo({this.name,this.reportStatus,this.infos});
factory LaunchInfo.fromJson(Map<String,dynamic> json){
var tempInfos = json["infos"] as List;
List<Info> infoList=tempInfos.map((e) => Info.fromJson(e)).toList();
return LaunchInfo(
name:json["name"].toString(),
reportStatus: json["reportStatus"],
infos:infoList,
);
}
}
带数据的json类型解析比较复杂,需要对list的进行进一步处理.
使用解析
HttpManager.getInstance().post<Map<String,dynamic>>(Api.launchInfo, map).then((value){
// var config = jsonDecode(value);
// SpUtil.getInstance().then((value) => value.)
setState(() {
info = LaunchInfo.fromJson(value);
_adUrl=info.infos.first.imageurl;
});
});
重点在于LaunchInfo.fromJson(value);是它吧http请求回来的map类型的数据转换成了具体bean