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]));
}

大概就是這個樣子了,可以節約很多時間,提高效率。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章