Flutter序列化⽀持泛型的坑请求接⼝得到的数据
{
"servegeneralist": [
{
"fwdlname": "健康服务",
"list": [
{
"fwxlid": "2c908084690da43b01690ed9e3b30004",
"fwxlname": "韩式SPA⽔疗",
"price": 100.0
},
{
"fwxlid": "402881d86914312b0169147271a10029",
"fwxlname": "泰式全⾝按摩",
"price": 300.0
},
{
"fwxlid": "402881d86931b22b0169331c4b790046",
"fwxlname": "医疗护理",
"price": 10.0
},
{
"fwxlid": "402881d86931b22b0169331dad710047",
"fwxlname": "康复保健",
"price": 10.0
},
{
"fwxlid": "402881d86931b22b0169331ea2970048",
"fwxlname": "健⾝养⽣",
"price": 10.0
}
]
},
{
"fwdlname": "家政服务",
"list": [
{
"fwxlid": "402881d86914312b01691470111b0026",
"fwxlname": "清洗⾐物",
"price": 100.0
},
{
"fwxlid": "402881d86914312b0169147076f90027",
"fwxlname": "打扫卫⽣",
"price": 200.0
},
{
"fwxlid": "402881d86931b22b01693322a6bf0049",
"fwxlname": "职业保姆",
"price": 10.0
},
{
"fwxlid": "402881d86931b22b01693323cdc8004e",
"fwxlname": "育婴早教",
"price": 10.0
},
{
"fwxlid": "402881d86931b22b016933288a9c0055",
"fwxlname": "玻璃清洗",
"price": 10.0
},
{
"fwxlid": "402881d86931b22b01693328dc7a0056",
"fwxlname": "地毯清洗",
"price": 10.0
}
]
}
],
"serverPackageList": [
{
"list": [
{
"intid": "40289fbe692268e7016922849000000b",
"serveritemname": "韩式SPA⽔疗,泰式全⾝按"
}
],
"serverPackageAmount": 120.0,
"serverpackagename": "养⽣套餐A"
},
{
"list": [
{
"intid": "40289fbe692268e701692285e71a000c",
"serveritemname": "清洗⾐物,打扫卫⽣"
}
],
"serverPackageAmount": 50.0,
"serverpackagename": "家居卫⽣"
},
{
"list": [
{
"intid": "40289fbe6922a47d016922cfa420004c",
"serveritemname": "韩式SPA⽔疗,泰式全⾝按摩,清洗⾐物,打扫卫⽣" }
],
"serverPackageAmount": 510.0,
"serverpackagename": "套餐1"
}
]
}
书写darBean
先看官⽹的序列化列⼦
pubspec.yaml 添加依赖
dependencies:
# Your other regular dependencies here
json_annotation: ^2.0.0
dev_dependencies:
# Your other dev_dependencies here
build_runner: ^1.0.0
json_serializable: ^2.0.0
模仿列⼦写 darBean
import 'package:json_annotation/json_annotation.dart';
part 'dart';
//服务项⽬(商家bean)
flutter pub//知道通⽤否
@JsonSerializable()
class ServerSusiness{
List <ServerGenBean>servegeneralist;
List <ServerPageBean>serverPackageList;
ServerSusiness(this.servegeneralist,this.serverPackageList);
// factory ServerSusiness.fromJson(Map<String,dynamic> json)=>_$ServerSusiness.fromJson(json);
factory ServerSusiness.fromJson(Map<String,dynamic> json)=>_$ServerSusinessFromJson(json);
Map<String,dynamic> toJson()=>_$ServerSusinessToJson(this);
}
@JsonSerializable()
class ServerPageBean {
double serverPackageAmount;
String serverpackagename;
List<SerpageBean_bean> list;
ServerPageBean(this.serverPackageAmount,this.serverpackagename,this.list);
// factory ServerPageBean.fromJson(Map<String,dynamic> json)=>_ServerPageBeanFromJson(json);
factory ServerPageBean.fromJson(Map<String,dynamic> json)=>_$ServerPageBeanFromJson(json);
Map<String,dynamic> toJson()=>_$ServerPageBeanToJson(this);
}
@JsonSerializable()
class SerpageBean_bean {
String intid;
String serveritemname ;
SerpageBean_bean(this.intid,this.serveritemname);
factory SerpageBean_bean.fromJson(Map<String,dynamic> json)=>_$SerpageBean_beanFromJson(json);
Map<String,dynamic> toJson()=>_$SerpageBean_beanToJson(this);
}
@JsonSerializable()
class ServerGenBean {
String fwdlname;
List<Servergean_bean_bean>list;
ServerGenBean(this.fwdlname,this.list);
factory ServerGenBean.fromJson(Map<String,dynamic> json)=>_$ServerGenBeanFromJson(json);
Map<String,dynamic> toJson()=>_$ServerGenBeanToJson(this);
}
@JsonSerializable()
class Servergean_bean_bean{
String fwxlid;
String fwxlname;
double price; //拿到数据是什么类型这⾥就定义什么样的类型
Servergean_bean_bean(this.fwxlid,this.fwxlname,this.price);
factory Servergean_bean_bean.fromJson(Map<String,dynamic> json)=>_$Servergean_bean_beanFromJson(json); Map<String,dynamic> toJson()=>_$Servergean_bean_beanToJson(this);
}
注意事项
上⾯还是要模仿 官⽹的列⼦
1、导包
import 'package:json_annotation/json_annotation.dart';
part 'dart'; //这个是执⾏命令⽣成的,我们要写上
2、我们格式化⼀下 我们的json字符串看⼀下结构
主Bean
class ServerSusiness{
List <ServerGenBean>servegeneralist;
List <ServerPageBean>serverPackageList;
ServerSusiness(this.servegeneralist,this.serverPackageList);
// factory ServerSusiness.fromJson(Map<String,dynamic> json)=>_$ServerSusiness.fromJson(json);
factory ServerSusiness.fromJson(Map<String,dynamic> json)=>_$ServerSusinessFromJson(json);
Map<String,dynamic> toJson()=>_$ServerSusinessToJson(this);
}
两个次Bean
@JsonSerializable()
class ServerGenBean {
String fwdlname;
List<Servergean_bean_bean>list;
ServerGenBean(this.fwdlname,this.list);
factory ServerGenBean.fromJson(Map<String,dynamic> json)=>_$ServerGenBeanFromJson(json);
Map<String,dynamic> toJson()=>_$ServerGenBeanToJson(this);
}
@JsonSerializable()
class ServerPageBean {
double serverPackageAmount;
String serverpackagename;
List<SerpageBean_bean> list;
ServerPageBean(this.serverPackageAmount,this.serverpackagename,this.list);
// factory ServerPageBean.fromJson(Map<String,dynamic> json)=>_ServerPageBeanFromJson(json);
factory ServerPageBean.fromJson(Map<String,dynamic> json)=>_$ServerPageBeanFromJson(json);
Map<String,dynamic> toJson()=>_$ServerPageBeanToJson(this);
}
次次Bean 两个
@JsonSerializable()
class Servergean_bean_bean{
String fwxlid;
String fwxlname;
double price; //拿到数据是什么类型这⾥就定义什么样的类型
Servergean_bean_bean(this.fwxlid,this.fwxlname,this.price);
factory Servergean_bean_bean.fromJson(Map<String,dynamic> json)=>_$Servergean_bean_beanFromJson(json); Map<String,dynamic> toJson()=>_$Servergean_bean_beanToJson(this);
}
@JsonSerializable()
class SerpageBean_bean {
String intid;
String serveritemname ;
SerpageBean_bean(this.intid,this.serveritemname);
factory SerpageBean_bean.fromJson(Map<String,dynamic> json)=>_$SerpageBean_beanFromJson(json);
Map<String,dynamic> toJson()=>_$SerpageBean_beanToJson(this);
}
书写完成后执⾏命令
flutter packages pub run build_runner build
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论