gpt4 book ai didi

android - 如何在Flutter中获取复杂的Json对象?

转载 作者:行者123 更新时间:2023-12-03 04:40:43 26 4
gpt4 key购买 nike

这是API响应,我想获取“分期付款”数组值

 {
"unit":
{
.....
},
"receipt":
{
.....
},
"instalments": [
{
"payment_type": "5",
"amount": "3",
"bank_name": "ABU DHABI COMMERCIAL BANK",
"paid_date": "2020-08-07 01:11:02",
"status": "0",
}
{
"payment_type": "5",
"amount": "3",
"bank_name": "ABU DHABI COMMERCIAL BANK",
"paid_date": "2020-08-07 01:11:02",
"status": "0",
}
]
}
我使用此模式类从API获取值(value)
class PaymentInstallment {
String payment_type;
String payment_amount;
String payment_date;
String status;

PaymentInstallment(
{this.payment_type, this.payment_amount, this.payment_date, this.status});

factory PaymentInstallment.fromjson(Map<String, dynamic> json) {
return PaymentInstallment(
payment_type: json['payment_type'],
payment_amount: json['amount'],
payment_date: json['paid_date'],
status: json['status'],
);
}
}
这是我的列表,并将服务器的响应添加到它
   List<PaymentInstallment> _list = [];


final response = await http.get(
url,
headers: {"Accept": "application/json", "Authorization": token},
);
var response = jsonDecode(response.body);
setState(() {
for (Map temppayment in response) {
_list.add(PaymentInstallment.fromjson(temppayment));
}
});
我怎样才能从“分期付款”数组中所有数组值并添加到列表中

最佳答案

试试这个

var response = jsonDecode(response.body);
List installmentList = List.from(response["installment"]);
setState((){
_list = installmentList.map((f) => PaymentInstallment.fromJson(f)).toList();
});
如果没有循环尝试此
List tempList = [];
installmentList.forEach((f) => tempList.add(f));
setState((){
_list = tempList;
});

关于android - 如何在Flutter中获取复杂的Json对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63305487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com