繁体   English   中英

如何在flutter dart上解析复杂的json

[英]How to parse complex json on flutter dart

我从复杂的 json 获取空数据,这是 json。 当我尝试解析这个时,我得到了空值。 请看看吹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"
    }

我已经生成模型类: https : //javiercbk.github.io/json_to_dart/

生成 Model 类后,创建一个新的类名 ModelClass。

另一个类名APiResponse并编写代码:该类获取api响应并返回响应:APiResponse.fromJson(jsonDecode(response.body));

我尝试解析:

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

模型类:

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

您可以使用此链接https://app.quicktype.io/从 json 生成模型类

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,
    };
}

我希望,这会奏效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM