Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

json to dart

class DataDTO {
  String? _name;
  String? _gender;
  int? _age;
  Address? _address;
  List<PhoneNumber>? _phoneNumber;

  DataDTO(
      {String? name,
      String? gender,
      int? age,
      Address? address,
      List<PhoneNumber>? phoneNumber}) {
    if (name != null) {
      this._name = name;
    }
    if (gender != null) {
      this._gender = gender;
    }
    if (age != null) {
      this._age = age;
    }
    if (address != null) {
      this._address = address;
    }
    if (phoneNumber != null) {
      this._phoneNumber = phoneNumber;
    }
  }

  String? get name => _name;
  set name(String? name) => _name = name;
  String? get gender => _gender;
  set gender(String? gender) => _gender = gender;
  int? get age => _age;
  set age(int? age) => _age = age;
  Address? get address => _address;
  set address(Address? address) => _address = address;
  List<PhoneNumber>? get phoneNumber => _phoneNumber;
  set phoneNumber(List<PhoneNumber>? phoneNumber) => _phoneNumber = phoneNumber;

  DataDTO.fromJson(Map<String, dynamic> json) {
    _name = json['name'];
    _gender = json['gender'];
    _age = json['age'];
    _address =
        json['address'] != null ? new Address.fromJson(json['address']) : null;
    if (json['phoneNumber'] != null) {
      _phoneNumber = <PhoneNumber>[];
      json['phoneNumber'].forEach((v) {
        _phoneNumber!.add(new PhoneNumber.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this._name;
    data['gender'] = this._gender;
    data['age'] = this._age;
    if (this._address != null) {
      data['address'] = this._address!.toJson();
    }
    if (this._phoneNumber != null) {
      data['phoneNumber'] = this._phoneNumber!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Address {
  String? _street;
  String? _city;
  String? _state;
  String? _postalCode;

  Address({String? street, String? city, String? state, String? postalCode}) {
    if (street != null) {
      this._street = street;
    }
    if (city != null) {
      this._city = city;
    }
    if (state != null) {
      this._state = state;
    }
    if (postalCode != null) {
      this._postalCode = postalCode;
    }
  }

  String? get street => _street;
  set street(String? street) => _street = street;
  String? get city => _city;
  set city(String? city) => _city = city;
  String? get state => _state;
  set state(String? state) => _state = state;
  String? get postalCode => _postalCode;
  set postalCode(String? postalCode) => _postalCode = postalCode;

  Address.fromJson(Map<String, dynamic> json) {
    _street = json['street'];
    _city = json['city'];
    _state = json['state'];
    _postalCode = json['postalCode'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['street'] = this._street;
    data['city'] = this._city;
    data['state'] = this._state;
    data['postalCode'] = this._postalCode;
    return data;
  }
}

class PhoneNumber {
  String? _type;
  String? _number;

  PhoneNumber({String? type, String? number}) {
    if (type != null) {
      this._type = type;
    }
    if (number != null) {
      this._number = number;
    }
  }

  String? get type => _type;
  set type(String? type) => _type = type;
  String? get number => _number;
  set number(String? number) => _number = number;

  PhoneNumber.fromJson(Map<String, dynamic> json) {
    _type = json['type'];
    _number = json['number'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['type'] = this._type;
    data['number'] = this._number;
    return data;
  }
}
Source by javiercbk.github.io #
 
PREVIOUS NEXT
Tagged: #json #dart
ADD COMMENT
Topic
Name
8+9 =