如何在 Flutter 中从复杂的 json 数据模型中检索数据

How to retrieve data from a complicated json data model in Flutter

Shippingmodel shippingmodelFromJson(String str) => Shippingmodel.fromJson(json.decode(str));

String shippingmodelToJson(Shippingmodel data) => json.encode(data.toJson());

class Shippingmodel {
  Shippingmodel({
    this.sellerShipping,
    this.result,
    this.shippingType,
    this.value,
    this.valueString,
  });

  Map<String, List<SellerShipping>> sellerShipping;
  bool result;
  String shippingType;
  int value;
  String valueString;

  factory Shippingmodel.fromJson(Map<String, dynamic> json) => Shippingmodel(
    sellerShipping: Map.from(json["seller_shipping"]).map((k, v) => MapEntry<String, List<SellerShipping>>(k, List<SellerShipping>.from(v.map((x) => SellerShipping.fromJson(x))))),
    result: json["result"],
    shippingType: json["shipping_type"],
    value: json["value"],
    valueString: json["value_string"],
  );

  Map<String, dynamic> toJson() => {
    "seller_shipping": Map.from(sellerShipping).map((k, v) => MapEntry<String, dynamic>(k, List<dynamic>.from(v.map((x) => x.toJson())))),
    "result": result,
    "shipping_type": shippingType,
    "value": value,
    "value_string": valueString,
  };
}

class SellerShipping {
  SellerShipping({
    this.code,
    this.name,
    this.price,
    this.deadline,
    this.erroMsg,
  });

  String code;
  String name;
  String price;
  String deadline;
  String erroMsg;

  factory SellerShipping.fromJson(Map<String, dynamic> json) => SellerShipping(
    code: json["code"],
    name: json["name"],
    price: json["price"],
    deadline: json["deadline"],
    erroMsg: json["erroMsg"],
  );

  Map<String, dynamic> toJson() => {
    "code": code,
    "name": name,
    "price": price,
    "deadline": deadline,
    "erroMsg": erroMsg,
  };
}

这是 json 数据模型。我对如何检索任何东西感到困惑,比如来自卖家运费的价格。我从存储库调用它并传递响应。 body。如有任何帮助,我们将不胜感激!

这是 json 数据模型。我对如何检索任何东西感到困惑,比如来自卖家运费的价格。我从存储库调用它并传递响应。 body。如有任何帮助,我们将不胜感激!

我假设你想要这个。如果您还有其他意思,请在此答案中发表评论。我会尽力帮忙的。

var json = jsonDecode(response.body);
Shippingmodel _shippingmodel = Shippingmodel.fromJson(json);
String _price = _shippingmodel.sellerShipping.price;

对于任何坚持这个问题的人,这个 post 的答案帮助了我,那个有 flutter 例子的。

Get the length of List that exists in Map<String,List<Object>> in Flutter