1

I am getting null data from a complex json, here's the json. When I have try to parse this I got null. Please have a look blow json.

    {
        "billInfoList": [
            {
                "accountBalance": 0,
                "address": {
                    "street": "nd",
                    "complement": "df",
                    "city": "dsfs",
                    "neighborhood": "cxcnx",
                    "isForcedAddress": false,
                    "state": "ARE",
                    "zip": "00000",
                    "country": "hchchc",
                    "district": "ccc"
                },
                "billDay": 0,
                "billFrequency": 0,
                "billNumber": "hcc",
                "billType": 1cxc1,
                "creationDate": "2019-02-04",
                "dueDate": "2019-02-19",
                "servicePeriodEnd": "2019-02-04",
                "servicePeriodStart": "2019-01-29",
                "status": "Paid",
                "value": 0,
                "valueDisputed": 0,
                "valueOpen": 0
            },
            {
                "accountBalance": 0,
                "address": {
                    "street": "cxcvenciones ",
                    "complement": "Test124",
                    "city": "Arequipa",
                    "neighborhood": "Test4",
                    "isForcedAddress": false,
                    "state": "ARE",
                    "zip": "00320",
                    "country": "PE",
                    "district": "cquipa"
                },
                "billDay": 0,
                "billFrequency": 0,
                "billNumber": "dfs678",
                "billType": fds1,
                "billURL": "http://",
                "creationDate": "2019-01-29",
                "dueDate": "2019-02-13",
                "servicePeriodEnd": "2019-01-29",
                "servicePeriodStart": "2019-01-29",
                "status": "Paid",
                "value": 3.6,
                "valueDisputed": 0,
                "valueOpen": 0
            },
            {
                "accountBalance": 0,
                "address": {
                    "street": "fsdnciones ",
                    "complement": "Test124",
                    "city": "dfspa",
                    "neighborhood": "Test4",
                    "isForcedAddress": false,
                    "state": "ARE",
                    "zip": "3200",
                    "country": "PE",
                    "district": "requipa"
                },
                "billDay": 0,
                "billFrequency": 0,
                "billNumber": "323677",
                "billType": 341,
                "creationDate": "2019-01-29",
                "dueDate": "2019-02-13",
                "servicePeriodEnd": "2019-01-29",
                "servicePeriodStart": "2019-01-29",
                "status": "Pd",
                "value": 0,
                "valueDisputed": 0,
                "valueOpen": 0
            }
        ],
        "TransactionSequenceId": "hrhrhrh9",
        "ResponseCode": "hfhf00",
        "ResponseMessage": "Request Processed successfully"
    }

I have generate Model class: https://javiercbk.github.io/json_to_dart/

After generate the Model class create a new class name ModelClass.

Another class name APiResponse and write the code: This class get api response and return response: APiResponse.fromJson(jsonDecode(response.body));

I have try to parse:

    APiResponse.fromJson(Map<String, dynamic> json)
          : results = new ModelClass.fromJson(json),          
            error = ""; 

ModelClass:

class ModelClass {
  List<BillInfoList> billInfoList;
  String transactionSequenceId;
  String responseCode;
  String responseMessage;

  ModelClass (
      {this.billInfoList,
      this.transactionSequenceId,
      this.responseCode,
      this.responseMessage});

  ModelClass.fromJson(Map<String, dynamic> json) {
    if (json['billInfoList'] != null) {
      billInfoList = new List<BillInfoList>();
      json['billInfoList'].forEach((v) {
        billInfoList.add(new BillInfoList.fromJson(v));
      });
    }
    transactionSequenceId = json['TransactionSequenceId'];
    responseCode = json['ResponseCode'];
    responseMessage = json['ResponseMessage'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.billInfoList != null) {
      data['billInfoList'] = this.billInfoList.map((v) => v.toJson()).toList();
    }
    data['TransactionSequenceId'] = this.transactionSequenceId;
    data['ResponseCode'] = this.responseCode;
    data['ResponseMessage'] = this.responseMessage;
    return data;
  }
}

class BillInfoList {
  int accountBalance;
  Address address;
  int billDay;
  int billFrequency;
  String billNumber;
  int billType;
  String creationDate;
  String dueDate;
  String servicePeriodEnd;
  String servicePeriodStart;
  String status;
  double value;
  int valueDisputed;
  int valueOpen;
  String billURL;

  BillInfoList(
      {this.accountBalance,
      this.address,
      this.billDay,
      this.billFrequency,
      this.billNumber,
      this.billType,
      this.creationDate,
      this.dueDate,
      this.servicePeriodEnd,
      this.servicePeriodStart,
      this.status,
      this.value,
      this.valueDisputed,
      this.valueOpen,
      this.billURL});

  BillInfoList.fromJson(Map<String, dynamic> json) {
    accountBalance = json['accountBalance'];
    address =
        json['address'] != null ? new Address.fromJson(json['address']) : null;
    billDay = json['billDay'];
    billFrequency = json['billFrequency'];
    billNumber = json['billNumber'];
    billType = json['billType'];
    creationDate = json['creationDate'];
    dueDate = json['dueDate'];
    servicePeriodEnd = json['servicePeriodEnd'];
    servicePeriodStart = json['servicePeriodStart'];
    status = json['status'];
    value = json['value'];
    valueDisputed = json['valueDisputed'];
    valueOpen = json['valueOpen'];
    billURL = json['billURL'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['accountBalance'] = this.accountBalance;
    if (this.address != null) {
      data['address'] = this.address.toJson();
    }
    data['billDay'] = this.billDay;
    data['billFrequency'] = this.billFrequency;
    data['billNumber'] = this.billNumber;
    data['billType'] = this.billType;
    data['creationDate'] = this.creationDate;
    data['dueDate'] = this.dueDate;
    data['servicePeriodEnd'] = this.servicePeriodEnd;
    data['servicePeriodStart'] = this.servicePeriodStart;
    data['status'] = this.status;
    data['value'] = this.value;
    data['valueDisputed'] = this.valueDisputed;
    data['valueOpen'] = this.valueOpen;
    data['billURL'] = this.billURL;
    return data;
  }
}

class Address {
  String street;
  String complement;
  String city;
  String neighborhood;
  bool isForcedAddress;
  String state;
  String zip;
  String country;
  String district;

  Address(
      {this.street,
      this.complement,
      this.city,
      this.neighborhood,
      this.isForcedAddress,
      this.state,
      this.zip,
      this.country,
      this.district});

  Address.fromJson(Map<String, dynamic> json) {
    street = json['street'];
    complement = json['complement'];
    city = json['city'];
    neighborhood = json['neighborhood'];
    isForcedAddress = json['isForcedAddress'];
    state = json['state'];
    zip = json['zip'];
    country = json['country'];
    district = json['district'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['street'] = this.street;
    data['complement'] = this.complement;
    data['city'] = this.city;
    data['neighborhood'] = this.neighborhood;
    data['isForcedAddress'] = this.isForcedAddress;
    data['state'] = this.state;
    data['zip'] = this.zip;
    data['country'] = this.country;
    data['district'] = this.district;
    return data;
  }
}
5
  • Could you please add ModelClass ? Commented Dec 11, 2019 at 13:18
  • Sunny, Please generate model class by javiercbk.github.io/json_to_dart Commented Dec 11, 2019 at 13:23
  • Number of character is height so I can't post on comment box Commented Dec 11, 2019 at 13:24
  • You can add it into your post just click on edit and then add the class Commented Dec 11, 2019 at 13:25
  • Done, Please have a look Commented Dec 11, 2019 at 13:26

1 Answer 1

1

You can generate model class from json using this link https://app.quicktype.io/

import 'dart:convert';

BillInfoList billInfoListFromJson(String str) => BillInfoList.fromJson(json.decode(str));

String billInfoListToJson(BillInfoList data) => json.encode(data.toJson());

class BillInfoList {
    List<BillInfoListElement> billInfoList;
    String transactionSequenceId;
    String responseCode;
    String responseMessage;

    BillInfoList({
        this.billInfoList,
        this.transactionSequenceId,
        this.responseCode,
        this.responseMessage,
    });

    factory BillInfoList.fromJson(Map<String, dynamic> json) => BillInfoList(
        billInfoList: List<BillInfoListElement>.from(json["billInfoList"].map((x) => BillInfoListElement.fromJson(x))),
        transactionSequenceId: json["TransactionSequenceId"],
        responseCode: json["ResponseCode"],
        responseMessage: json["ResponseMessage"],
    );

    Map<String, dynamic> toJson() => {
        "billInfoList": List<dynamic>.from(billInfoList.map((x) => x.toJson())),
        "TransactionSequenceId": transactionSequenceId,
        "ResponseCode": responseCode,
        "ResponseMessage": responseMessage,
    };
}

class BillInfoListElement {
    int accountBalance;
    Address address;
    int billDay;
    int billFrequency;
    String billNumber;
    dynamic billType;
    DateTime creationDate;
    DateTime dueDate;
    DateTime servicePeriodEnd;
    DateTime servicePeriodStart;
    String status;
    double value;
    int valueDisputed;
    int valueOpen;
    String billUrl;

    BillInfoListElement({
        this.accountBalance,
        this.address,
        this.billDay,
        this.billFrequency,
        this.billNumber,
        this.billType,
        this.creationDate,
        this.dueDate,
        this.servicePeriodEnd,
        this.servicePeriodStart,
        this.status,
        this.value,
        this.valueDisputed,
        this.valueOpen,
        this.billUrl,
    });

    factory BillInfoListElement.fromJson(Map<String, dynamic> json) => BillInfoListElement(
        accountBalance: json["accountBalance"],
        address: Address.fromJson(json["address"]),
        billDay: json["billDay"],
        billFrequency: json["billFrequency"],
        billNumber: json["billNumber"],
        billType: json["billType"],
        creationDate: DateTime.parse(json["creationDate"]),
        dueDate: DateTime.parse(json["dueDate"]),
        servicePeriodEnd: DateTime.parse(json["servicePeriodEnd"]),
        servicePeriodStart: DateTime.parse(json["servicePeriodStart"]),
        status: json["status"],
        value: json["value"].toDouble(),
        valueDisputed: json["valueDisputed"],
        valueOpen: json["valueOpen"],
        billUrl: json["billURL"] == null ? null : json["billURL"],
    );

    Map<String, dynamic> toJson() => {
        "accountBalance": accountBalance,
        "address": address.toJson(),
        "billDay": billDay,
        "billFrequency": billFrequency,
        "billNumber": billNumber,
        "billType": billType,
        "creationDate": "${creationDate.year.toString().padLeft(4, '0')}-${creationDate.month.toString().padLeft(2, '0')}-${creationDate.day.toString().padLeft(2, '0')}",
        "dueDate": "${dueDate.year.toString().padLeft(4, '0')}-${dueDate.month.toString().padLeft(2, '0')}-${dueDate.day.toString().padLeft(2, '0')}",
        "servicePeriodEnd": "${servicePeriodEnd.year.toString().padLeft(4, '0')}-${servicePeriodEnd.month.toString().padLeft(2, '0')}-${servicePeriodEnd.day.toString().padLeft(2, '0')}",
        "servicePeriodStart": "${servicePeriodStart.year.toString().padLeft(4, '0')}-${servicePeriodStart.month.toString().padLeft(2, '0')}-${servicePeriodStart.day.toString().padLeft(2, '0')}",
        "status": status,
        "value": value,
        "valueDisputed": valueDisputed,
        "valueOpen": valueOpen,
        "billURL": billUrl == null ? null : billUrl,
    };
}

class Address {
    String street;
    String complement;
    String city;
    String neighborhood;
    bool isForcedAddress;
    String state;
    String zip;
    String country;
    String district;

    Address({
        this.street,
        this.complement,
        this.city,
        this.neighborhood,
        this.isForcedAddress,
        this.state,
        this.zip,
        this.country,
        this.district,
    });

    factory Address.fromJson(Map<String, dynamic> json) => Address(
        street: json["street"],
        complement: json["complement"],
        city: json["city"],
        neighborhood: json["neighborhood"],
        isForcedAddress: json["isForcedAddress"],
        state: json["state"],
        zip: json["zip"],
        country: json["country"],
        district: json["district"],
    );

    Map<String, dynamic> toJson() => {
        "street": street,
        "complement": complement,
        "city": city,
        "neighborhood": neighborhood,
        "isForcedAddress": isForcedAddress,
        "state": state,
        "zip": zip,
        "country": country,
        "district": district,
    };
}

I hope ,this will work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.