如何将 json 列表传递给 Flutter 中的 http 请求(post)正文?

Posted

技术标签:

【中文标题】如何将 json 列表传递给 Flutter 中的 http 请求(post)正文?【英文标题】:How to pass a list of json to body of http request (post) in Flutter? 【发布时间】:2020-01-24 20:25:05 【问题描述】:

我有一些对象将由用户填写在表单中。我将这些对象解析为 json 并将该 json 添加到列表中以传入请求正文。但我不能这样做。

  incrementListPaymentSlipes(PaymentSlipes objPayment) async 
    objPayment.name = "Douglas";
    objPayment.personalId = "00000000000";
    Map<String, dynamic> json = objPayment.toJson();
    listPaymentSlipes.add(jsonEncode(json));
  
var response = await http.post(url, body: 
"payment_slips": listPaymentSlipes,

正确正文示例:

"payment_slips": [
          
      "personal_id": "01888728680",
      "name": "Fulano da Silva"
    
  ]
 "error":"'personal_id: 00000000000, name: Douglas' é invalido como 'payment_slips'","code":"payment_slips_invalid"```

【问题讨论】:

【参考方案1】:

您可以通过一种非常简单的方式来做到这一点。创建payment.dart文件并复制粘贴下面的代码类。

class PaymentList 
PaymentList(this.payments);

List<Payment> payments;

Map<String, dynamic> toJson() => <String, dynamic>
    'payment_slips': payments,
  ;


class Payment 
Payment(this.name, this.personalId);

String name;
String personalId;

Map<String, dynamic> toJson() => <String, dynamic>
    'personal_id': personalId,
    'name': name,
  ;

现在您可以使用以下代码将其转换为所需的 json 格式。例如我正在创建一个虚拟列表:

final PaymentList paymentList =
    PaymentList(List<Payment>.generate(2, (int index) 
  return Payment(name: 'Person $index', personalId: '$index');
));
final String requestBody = json.encoder.convert(paymentList);

requestBody 变量将具有如下的 json 字符串:

  "payment_slips": [
    
        "personal_id": "0",
        "name": "Person 0"
    ,
    
        "personal_id": "1",
        "name": "Person 1"
    
  ]

现在你可以调用api了:

var response = await http.post(url, body: requestBody

注意:请导入以下包,这是访问json所必需的:

import 'dart:convert';

【讨论】:

【参考方案2】:

所以看起来你没有得到你期望的 JSON。我已经整理了一些代码来告诉你如何得到你想要的身体。

在 DartPad 中运行的链接https://dartpad.dartlang.org/3fde03078e56efe13d31482dea8e5eef

    class PaymentSlipes 
      String name;
      String personaId;

      ObjPayment(this.name, this.personaId);

      //You create this to convert your object to JSON
      Map<String, dynamic> toJson() => 'name': name, 'personaId': personaId;
    

    // This method is needed to convert the list of ObjPayment into an Json Array
    List encondeToJson(List<PaymentSlipes> list) 
      List jsonList = List();
      list.map((item) => jsonList.add(item.toJson())).toList();
      return jsonList;
    

    // This is an example and this code will run in DartPad link above
    void main() 
      PaymentSlipes objPayment = PaymentSlipes(name: "Douglas", personaId: "123425465");
      PaymentSlipes objPayment2 = PaymentSlipes(name: "Dave", personaId: "123425465;
      PaymentSlipes objPayment3 = PaymentSlipes(name: "Mike", personaId: "123425465");
      var list = [objPayment, objPayment2, objPayment3];

      // This is the mapping of the list under the key payment_slips as per your example and the body i would pass to the POST
      var finalJson = "payment_slips": encondeToJson(list);
      print(finalJson);
    

【讨论】:

以上是关于如何将 json 列表传递给 Flutter 中的 http 请求(post)正文?的主要内容,如果未能解决你的问题,请参考以下文章

如何将反序列化JSON中的列表传递给视图到选择列表

Flutter:如何将值从 streamprovider / listview builder 传递给另一个类

如何将值从initstate传递给flutter中的小部件

如何将 ArrayList 项传递给 Flutter 中的 http 请求(post)正文?

如何将值列表传递到 json+sparksql 中的 rdd

Flutter:如何将列表传递到 Firestore where 条件