DART - 过滤 JSON 数据

DART - Filter JSON Data

我正在尝试过滤 json 数据。我的 json 中有一个名为“品牌”的字段(所以基本上我正在尝试按品牌过滤数据)

这就是我的 json 的样子

{
  "items": [
    {
      "_id": "30baa1ca-4186-4ff0-abe8-a5970e753444",
      "_owner": "1d3480e5-0eda-47ef-8406-38d89bf15ded",
      "_createdDate": "2022-05-09T08:47:29.137Z",
      "discountedPrice": "44.97",
      "_updatedDate": "2022-05-09T08:48:44.147Z",
      "getDealLink": "https://amzn.to/3FqBq4O",
      "brands": [
        "Amazon"
      ],
      "title": "Mellanni Extra Deep Pocket Twin XL Sheet Set ",
      "amazonlogo": "wix:image://v1/1d3480_ffad681242174f799ddea471e649ef7b~mv2.png/amazon_PNG24.png#originWidth=1024&originHeight=346",
      "save": "#1 Best Seller",
      "link-items-all": "/items/",
      "link-items-title": "/items/mellanni-extra-deep-pocket-twin-xl-sheet-set-"
    },
    {
      "_id": "a7d3aaa8-9654-4535-b6c5-b147ff0d8eb3",
      "_owner": "1d3480e5-0eda-47ef-8406-38d89bf15ded",
      "_createdDate": "2022-05-08T22:35:38.398Z",
      "discountedPrice": ".59",
      "_updatedDate": "2022-05-08T22:39:52.801Z",
      "getDealLink": "https://amzn.to/3ymXGLe",
      "brands": [
        "Amazon"
      ],
      "originalPrice": "9.99",
      "title": "2 Pack Stadium chairs for bleachers with back support",
      "amazonlogo": "wix:image://v1/1d3480_ffad681242174f799ddea471e649ef7b~mv2.png/amazon_PNG24.png#originWidth=1024&originHeight=346",
      "link-items-all": "/items/",
      "link-items-title": "/items/2-pack-stadium-chairs-for-bleachers-with-back-support"
    },

这是我的飞镖代码

  void getAmazon() async {
    var response = await http.get(Uri.parse(url));
    var decodeResponse = jsonDecode(response.body);
    List data = decodeResponse['items'] as List;
    Iterable filteredData = data.where((element) => element['brands'][0] == 'Amazon');

    print(filteredData); // returns nothing
  }

它没有 return/print 任何东西。我做错了什么?

最好使用 contains 检查品牌是否已列出。还要检查“品牌”字段是否可用以获得更好的稳定性。

    final filteredData = data.where((element) => (element['brands'] != null ? element['brands'].contains('Amazon') : false));

在您的代码中,您正在检查 brands 是否等于 Amazon,但 brands 实际上是一个列表。 (或者在您检查特定索引的情况下,这可能会改变)

所以["Amazon"]Amazon.

在下面的代码中,您现在将检查 brands 是否包含“Amazon”。

Iterable filteredData = data.where((element) => element['brands'].contains('Amazon'));
void getAmazon() async {
  var response = await http.get(Uri.parse('https://mockbin.org/bin/e123b53d-6e35-49e7-a94e-f49554a63d7e'));
  var decodeResponse = jsonDecode(response.body);
  List data = decodeResponse['items'] as List;
  Iterable filteredData = data.where((element) => element['brands'][0] == 'Amazon');

  log('ddf ${filteredData}'); // returns nothing
}

I had added third product as brand from flipkart it filter this! you can check this url https://mockbin.org/bin/e123b53d-6e35-49e7-a94e-f49554a63d7e Your code actually works as expected!!!

[log] ddf ({_id: 30baa1ca-4186-4ff0-abe8-a5970e753444, _owner: 1d3480e5-0eda-47ef-8406-38d89bf15ded, _createdDate: 2022-05-09T08:47:29.137Z, discountedPrice: 44.97, _updatedDate: 2022-05-09T08:48:44.147Z, getDealLink: https://amzn.to/3FqBq4O, brands: [Amazon], title: Mellanni Extra Deep Pocket Twin XL Sheet Set , amazonlogo: wix:image://v1/1d3480_ffad681242174f799ddea471e649ef7b~mv2.png/amazon_PNG24.png#originWidth=1024&originHeight=346, save: #1 Best Seller, link-items-all: /items/, link-items-title: /items/mellanni-extra-deep-pocket-twin-xl-sheet-set-}, {_id: a7d3aaa8-9654-4535-b6c5-b147ff0d8eb3, _owner: 1d3480e5-0eda-47ef-8406-38d89bf15ded, _createdDate: 2022-05-08T22:35:38.398Z, discountedPrice: .59, _updatedDate: 2022-05-08T22:39:52.801Z, getDealLink: https://amzn.to/3ymXGLe, brands: [Amazon], originalPrice: 9.99, title: 2 Pack Stadium chairs for bleachers with back support, amazonlogo: wix:image://v1/1d3480_ffad681242174f799ddea471e649ef7b~mv2.png/amazon_PNG24.png#originWidth=1024&originHeight=346, link-items-all: /items/, link-items-title: /items/2-pack-stadium-chairs-for-bleachers-with-back-support})