【问题标题】:How to parse json with the diffrent key in flutter如何在flutter中使用不同的键解析json
【发布时间】:2021-06-25 07:35:48
【问题描述】:

当我从服务器获得 JSON 响应时,我创建了使用来自服务器的 api 的颤振应用程序我解析响应并检索病假、服务名称、日期、用户、状态和一些关于病假的更多信息,问题是一些JSON 响应中的病患没有 doctororder 键或 medicalImagingOrder 键并且具有不同的键。当我尝试获取这些 JSON 键值时,程序会抛出一个错误,我应该如何处理?

这就是我创建对象的方式

class Datum {
  Datum({
    this.id,
    this.sicke,
    this.serviceName,
    this.doctorOrder,
    this.date,
    this.user,
    this.statusNumber,
    this.status,
    this.statusHistory,
    this.insertDate,
    this.statuses,
    this.excessInvoice,
    this.price,
    this.shippingCosts,
    this.vat,
    this.netPrice,
    this.totalPrice,
    this.orderNumber,
    this.v,
    this.cancleReason,
    this.medicalImagingOrder,
    this.laboratoryOrder,
    this.serviceProvider,
    this.nurseOrder,
  });

  String id;
  Sicke sicke;
  String serviceName;
  DoctorOrder doctorOrder;
  DateTime date;
  User user;
  int statusNumber;
  StatusEnum status;
  List<StatusHistory> statusHistory;
  DateTime insertDate;
  List<StatusElement> statuses;
  List<dynamic> excessInvoice;
  int price;
  int shippingCosts;
  int vat;
  int netPrice;
  int totalPrice;
  int orderNumber;
  int v;
  String cancleReason;
  Order medicalImagingOrder;
  LaboratoryOrder laboratoryOrder;
  String serviceProvider;
  Order nurseOrder;

  factory Datum.fromJson(Map<String, dynamic> json) => Datum(
    id: json["_id"],
    sicke: json["sicke"] == null ? null : Sicke.fromJson(json["sicke"]),
    serviceName: json["serviceName"],
    doctorOrder: json["doctorOrder"] == null ? null : DoctorOrder.fromJson(json["doctorOrder"]),
    date: DateTime.parse(json["date"]),
    user: userValues.map[json["user"]],
    statusNumber: json["statusNumber"],
    status: statusEnumValues.map[json["status"]],
    statusHistory: List<StatusHistory>.from(json["statusHistory"].map((x) => StatusHistory.fromJson(x))),
    insertDate: DateTime.parse(json["insertDate"]),
    statuses: List<StatusElement>.from(json["statuses"].map((x) => StatusElement.fromJson(x))),
    excessInvoice: List<dynamic>.from(json["excessInvoice"].map((x) => x)),
    price: json["price"],
    shippingCosts: json["shippingCosts"],
    vat: json["VAT"],
    netPrice: json["netPrice"],
    totalPrice: json["totalPrice"],
    orderNumber: json["orderNumber"],
    v: json["__v"],
    cancleReason: json["cancleReason"] == null ? null : json["cancleReason"],
    medicalImagingOrder: json["medicalImagingOrder"] == null ? null : Order.fromJson(json["medicalImagingOrder"]),
    laboratoryOrder: json["laboratoryOrder"] == null ? null : LaboratoryOrder.fromJson(json["laboratoryOrder"]),
    serviceProvider: json["serviceProvider"] == null ? null : json["serviceProvider"],
    nurseOrder: json["nurseOrder"] == null ? null : Order.fromJson(json["nurseOrder"]),
  );

这是我的 json 响应

【问题讨论】:

  • 您收到了对象列表? `基准`列表?
  • 你能分享一个错误日志吗?
  • 你可以试试这个解决方案。 stackoverflow.com/a/66632608/1737201
  • 是的,我收到了@mezoni 对象列表
  • @MasoudH 答案描述了对象列表的解决方案。 final list = response.body.jsonList((e) =&gt; Datum.fromJson(e)); 这比你想象的要容易。

标签: android json flutter dart


【解决方案1】:

您的 json 响应中有一个 Datum 列表。所以你可以从:

List<Datum> datumFromJson(String str) => List<Datum>.from(json.decode(str).map((x) => Datum.fromJson(x)));
String datumToJson(List<Datum> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

之后就可以开始上课了:

class Datum {
Datum({
    this.id,
    this.sicke,
    this.serviceName,
    this.doctorOrder,
    this.date,
    this.user,
    this.statusNumber,
    this.status,
    this.statusHistory,
    this.insertDate,
    this.statuses,
    this.excessInvoice,
    this.price,
    this.shippingCosts,
    this.vat,
    this.netPrice,
    this.totalPrice,
    this.orderNumber,
    this.v,
    this.cancleReason,
    this.medicalImagingOrder,
    this.laboratoryOrder,
    this.serviceProvider,
    this.nurseOrder,
});

String id;
LaboratoryOrder sicke;
String serviceName;
String doctorOrder;
String date;
LaboratoryOrder user;
int statusNumber;
LaboratoryOrder status;
List<LaboratoryOrder> statusHistory;
String insertDate;
List<LaboratoryOrder> statuses;
List<LaboratoryOrder> excessInvoice;
int price;
int shippingCosts;
int vat;
int netPrice;
int totalPrice;
int orderNumber;
int v;
String cancleReason;
LaboratoryOrder medicalImagingOrder;
LaboratoryOrder laboratoryOrder;
String serviceProvider;
LaboratoryOrder nurseOrder;

factory Datum.fromJson(Map<String, dynamic> json) => Datum(
    id: json["id"],
    sicke: LaboratoryOrder.fromJson(json["sicke"]),
    serviceName: json["serviceName"],
    doctorOrder: json["doctorOrder"],
    date: json["date"],
    user: LaboratoryOrder.fromJson(json["user"]),
    statusNumber: json["statusNumber"],
    status: LaboratoryOrder.fromJson(json["status"]),
    statusHistory: List<LaboratoryOrder>.from(json["statusHistory"].map((x) => LaboratoryOrder.fromJson(x))),
    insertDate: json["insertDate"],
    statuses: List<LaboratoryOrder>.from(json["statuses"].map((x) => LaboratoryOrder.fromJson(x))),
    excessInvoice: List<LaboratoryOrder>.from(json["excessInvoice"].map((x) => LaboratoryOrder.fromJson(x))),
    price: json["price"],
    shippingCosts: json["shippingCosts"],
    vat: json["vat"],
    netPrice: json["netPrice"],
    totalPrice: json["totalPrice"],
    orderNumber: json["orderNumber"],
    v: json["v"],
    cancleReason: json["cancleReason"],
    medicalImagingOrder: LaboratoryOrder.fromJson(json["medicalImagingOrder"]),
    laboratoryOrder: LaboratoryOrder.fromJson(json["laboratoryOrder"]),
    serviceProvider: json["serviceProvider"],
    nurseOrder: LaboratoryOrder.fromJson(json["nurseOrder"]),
);

Map<String, dynamic> toJson() => {
    "id": id,
    "sicke": sicke.toJson(),
    "serviceName": serviceName,
    "doctorOrder": doctorOrder,
    "date": date,
    "user": user.toJson(),
    "statusNumber": statusNumber,
    "status": status.toJson(),
    "statusHistory": List<dynamic>.from(statusHistory.map((x) => x.toJson())),
    "insertDate": insertDate,
    "statuses": List<dynamic>.from(statuses.map((x) => x.toJson())),
    "excessInvoice": List<dynamic>.from(excessInvoice.map((x) => x.toJson())),
    "price": price,
    "shippingCosts": shippingCosts,
    "vat": vat,
    "netPrice": netPrice,
    "totalPrice": totalPrice,
    "orderNumber": orderNumber,
    "v": v,
    "cancleReason": cancleReason,
    "medicalImagingOrder": medicalImagingOrder.toJson(),
    "laboratoryOrder": laboratoryOrder.toJson(),
    "serviceProvider": serviceProvider,
    "nurseOrder": nurseOrder.toJson(),
};}

最后是你的特定对象的一个​​类:

class LaboratoryOrder {
LaboratoryOrder();

factory LaboratoryOrder.fromJson(Map<String, dynamic> json) => LaboratoryOrder(
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2020-11-08
    • 2022-01-25
    • 2020-09-24
    • 2019-08-31
    • 1970-01-01
    相关资源
    最近更新 更多