flutter项目总结三(JSON对象)

首先推荐一个工具,名字叫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]));
}

大概就是这个样子了,可以节约很多时间,提高效率。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚俊飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值