首先推荐一个工具,名字叫Formatter_win.exe,网上可以下载到,有了这个工具,下面的事情就简单多了。
安装好图标是这个。
打开,左侧复制json数据进去,点击格式化,需要补充一些类名啥的。
然后点击右侧上面的生成bean,如下
最后,我把生成的代码贴出来:
import 'dart:convert' show json;
class Info {
int id;
int unit_nature_id;
int unit_type_id;
String address;
String code;
String control_room_tel;
String create_time;
String dimension;
String image;
String leader;
String leader_phone;
String legal_person;
String legal_person_phone;
String longitude;
String name;
UnitNature unitnature;
UnitType unittype;
Info.fromParams({this.id, this.unit_nature_id, this.unit_type_id, this.address, this.code, this.control_room_tel, this.create_time, this.dimension, this.image, this.leader, this.leader_phone, this.legal_person, this.legal_person_phone, this.longitude, this.name, this.unitnature, this.unittype});
factory Info(jsonStr) => jsonStr == null ? null : jsonStr is String ? new Info.fromJson(json.decode(jsonStr)) : new Info.fromJson(jsonStr);
Info.fromJson(jsonRes) {
id = jsonRes['id'];
unit_nature_id = jsonRes['unit_nature_id'];
unit_type_id = jsonRes['unit_type_id'];
address = jsonRes['address'];
code = jsonRes['code'];
control_room_tel = jsonRes['control_room_tel'];
create_time = jsonRes['create_time'];
dimension = jsonRes['dimension'];
image = jsonRes['image'];
leader = jsonRes['leader'];
leader_phone = jsonRes['leader_phone'];
legal_person = jsonRes['legal_person'];
legal_person_phone = jsonRes['legal_person_phone'];
longitude = jsonRes['longitude'];
name = jsonRes['name'];
unitnature = jsonRes['unitnature'] == null ? null : new UnitNature.fromJson(jsonRes['unitnature']);
unittype = jsonRes['unittype'] == null ? null : new UnitType.fromJson(jsonRes['unittype']);
}
@override
String toString() {
return '{"id": $id,"unit_nature_id": $unit_nature_id,"unit_type_id": $unit_type_id,"address": ${address != null?'${json.encode(address)}':'null'},"code": ${code != null?'${json.encode(code)}':'null'},"control_room_tel": ${control_room_tel != null?'${json.encode(control_room_tel)}':'null'},"create_time": ${create_time != null?'${json.encode(create_time)}':'null'},"dimension": ${dimension != null?'${json.encode(dimension)}':'null'},"image": ${image != null?'${json.encode(image)}':'null'},"leader": ${leader != null?'${json.encode(leader)}':'null'},"leader_phone": ${leader_phone != null?'${json.encode(leader_phone)}':'null'},"legal_person": ${legal_person != null?'${json.encode(legal_person)}':'null'},"legal_person_phone": ${legal_person_phone != null?'${json.encode(legal_person_phone)}':'null'},"longitude": ${longitude != null?'${json.encode(longitude)}':'null'},"name": ${name != null?'${json.encode(name)}':'null'},"unitnature": $unitnature,"unittype": $unittype}';
}
}
class UnitType {
int id;
String name;
UnitType.fromParams({this.id, this.name});
UnitType.fromJson(jsonRes) {
id = jsonRes['id'];
name = jsonRes['name'];
}
@override
String toString() {
return '{"id": $id,"name": ${name != null?'${json.encode(name)}':'null'}}';
}
}
class UnitNature {
int id;
String name;
UnitNature.fromParams({this.id, this.name});
UnitNature.fromJson(jsonRes) {
id = jsonRes['id'];
name = jsonRes['name'];
}
@override
String toString() {
return '{"id": $id,"name": ${name != null?'${json.encode(name)}':'null'}}';
}
}
初始化:
for (int i = 0; i < data['infolist'].length; i++) {
_unitList.add(UnitInfo(data['infolist'][i]));
}
大概就是这个样子了,可以节约很多时间,提高效率。