Dart-通过json值过滤数组

时间:2019-09-22 12:34:45

标签: json flutter dart

我有这个JSON响应:

{
    "suggestions": [
        {
            "label": "United States, <b>New York</b>",
            "language": "en",
            "countryCode": "USA",
            "locationId": "NT_rNsDLtnazM.kjVC-K6YWOA",
            "address": {
                "country": "United States",
                "state": "<b>New York</b>"
            },
            "matchLevel": "state"
        },
        {
            "label": "United States, <b>New York</b>, <b>New York</b>, <b>New York</b>",
            "language": "en",
            "countryCode": "USA",
            "locationId": "NT_YpQcXqbaOb.I4m5EW8BHEC",
            "address": {
                "country": "United States",
                "state": "<b>New York</b>",
                "county": "<b>New York</b>",
                "city": "<b>New York</b>",
                "postalCode": "10007"
            },
            "matchLevel": "city"
        },
        {
            "label": "United States, <b>New York</b>, <b>New York</b>",
            "language": "en",
            "countryCode": "USA",
            "locationId": "NT_ABhedB4xGEL83YY5az47iD",
            "address": {
                "country": "United States",
                "state": "<b>New York</b>",
                "county": "<b>New York</b>"
            },
            "matchLevel": "county"
        },
        {
            "label": "United Kingdom, NE27 0, North Shields, <b>New York</b> Way",
            "language": "en",
            "countryCode": "GBR",
            "locationId": "NT_B24AwYSzW4TK04x9UiKMoD",
            "address": {
                "country": "United Kingdom",
                "state": "England",
                "county": "Tyne and Wear",
                "city": "North Shields",
                "district": "North Shields",
                "street": "<b>New York</b> Way",
                "postalCode": "NE27 0"
            },
            "matchLevel": "street"
        },
        {
            "label": "United States, <b>New York</b>, Buffalo, <b>New York</b> State Thruway",
            "language": "en",
            "countryCode": "USA",
            "locationId": "NT_-WyF-KGZ-pqF8zQV6NegHD",
            "address": {
                "country": "United States",
                "state": "<b>New York</b>",
                "county": "Erie",
                "city": "Buffalo",
                "district": "Grant Ferry",
                "street": "<b>New York</b> State Thruway",
                "postalCode": "14213"
            },
            "matchLevel": "street"
        }
    ]
}

在代码中,我只想显示matchLevel类型为city的值。

到目前为止,我一直在尝试这种方法,但是它不起作用。我在这里做什么错了?

var data = json.decode(res.body);
      var rest = data["suggestions"] as List;
      var filteredList;
      rest.forEach((val) => filteredList.add(rest.where((val) => val["matchLevel"] == "city")));
      print(filteredList);

      this.list =
          filteredList.map<Suggestions>((json) => Suggestions.fromJson(json)).toList();

也尝试过:

1 个答案:

答案 0 :(得分:1)

更改此:

var data = json.decode(res.body);
      var rest = data["suggestions"] as List;
      var filteredList;
      rest.forEach((val) => filteredList.add(rest.where((val) => val["matchLevel"] == "city")));
      print(filteredList);

对此:

var data = json.decode(res.body);
      var rest = data["suggestions"] as List;
      var filteredList;
      filteredList = rest.where((val) => val["matchLevel"] == "city");
      print(filteredList);

来自docs

  

此方法返回映射元素的视图。只要未迭代返回的Iterable,将不会调用提供的函数test。迭代不会缓存结果,因此对返回的Iterable进行多次迭代可能会在同一元素上多次调用提供的功能测试。

因此,当您使用forEach()where()时,会多次获得相同的结果。