【问题标题】:I want to convert Object list to json array in flutter我想在颤动中将对象列表转换为 json 数组
【发布时间】:2021-03-05 21:40:45
【问题描述】:

我想将 List<Object> 转换为 List<List<dynamic>> 并将其作为 JSON 发送到 API。

这是我的对象模型类CartMapModel

class CartMapModel { 
  String itemName;
  String itemPrice;
  String itemDescription;
  
  CartMapModel({this.itemDescription,this.itemPrice,this.itemName});
  
  Map<String, dynamic> toJson() {
    return {
      "itemName": itemName,
      "itemPrice": itemPrice,
      "itemDescription" : itemDescription
    };
  }
}

这是我的List&lt;CartMapModel&gt;

cartOrderList.add(
  CartMapModel(
    itemPrice: itmPrice.toString(),
    itemName: itmName,
    itemDescription: itmDesc,
  )
);

我想把它转换成List&lt;List&lt;dynamic&gt;&gt;:

[
  [
    "Item Name 1",
    "123",
    "Item 1 Description"
  ],
  [
    "Item Name 2",
    "456",
    "Item 2 Description"
  ],
  [
    "Item Name 3",
    "789",
    "Item 3 Description"
  ]
]

【问题讨论】:

    标签: flutter dart flutter-dependencies


    【解决方案1】:

    这是你要找的吗?

    List<Map<String, dynamic>> jsonDataList =
        cartOrderList.map((cartOrder) => cartOrder.toJson().values.toList()).toList();
    

    【讨论】:

    • 不,它是在地图中转换它。我不需要地图。实际上我想删除 map 的键,只需要数组中的值,您可以看到上面的示例数组
    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多