'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,
};
}
This is the json data model. I'm perplexed as to how to retrieve anything, say price from sellerShipping. I'm calling it from a repository and passing the response. body. Any help would be appreciated!
This is the json data model. I'm perplexed as to how to retrieve anything, say price from sellerShipping. I'm calling it from a repository and passing the response. body. Any help would be appreciated!
Solution 1:[1]
i assume you want this. if you mean something else, comment in this answer. i will try to help.
var json = jsonDecode(response.body);
Shippingmodel _shippingmodel = Shippingmodel.fromJson(json);
String _price = _shippingmodel.sellerShipping.price;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hadi |
