【问题标题】:Remove duplicates from Json response flutter从 Json 响应颤动中删除重复项
【发布时间】:2019-08-14 19:06:48
【问题描述】:

我无法删除 Json 中的重复项。在下面的 Json 中,如果标题字段具有相同的值,我需要删除重复的节点。在名为“汽车”的 json 标题下方的 IN 重复了 3 次,我需要根据标题字段删除重复项。我关注了下面的stackoverflow链接,但没有解决我的问题。

Remove duplicate value in dart

实际的 Json:

{
    "totalSize": 6,
    "done": true,
    "records": [{
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "car",
            "Team_Category__c": "Vehicle"
        },
        {
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "car",
            "Team_Category__c": "Vehicle"
        },

        {
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "cycle",
            "Team_Category__c": "Vehicle"
        },
        {
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "aeroplane",
            "Team_Category__c": "Vehicle"
        },
        {
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "car",
            "Team_Category__c": "Vehicle"
        }
    ]
}

【问题讨论】:

  • 你能描述一下与颤振的关系吗?如果您从数据库中获取此 json 作为响应,您只需读出您需要的数据,无需删除任何内容...
  • 在我的数据库中,我遇到了一些具有相同名称的标题字段重复的问题。从移动应用程序 ios flutter 解析 json 时,我想使用 dart 中的 set 功能操作 Json 以删除重复项。下面的链接也类似于我的问题,但没有为我解决。 stackoverflow.com/questions/54087857/…
  • 如果您将其转换为地图,则会删除重复项
  • 能否分享如下示例代码 var newUniqueResponse = jsonResponseArray.map((records) => [records.title]);

标签: flutter


【解决方案1】:

kt_dart: 添加到您的 pubspec。

然后将其导入你的 dart 文件import 'package:kt_dart/kt.dart';

然后:

var json = jsonDecode(yourJsonAsString);
var records = mutableListFrom(json["records"]);
var distinct = records.distinctBy((it) => it["Title"]);

【讨论】:

    【解决方案2】:

    如果没有任何效果,您可以创建新的类似结构并只添加不同的项目,如下所示。我不是飞镖程序员,但这是一个幼稚的解决方案。

    导入 'dart:convert';

    void main() {

    var jsonData = '[{ "name" : "Dane", "alias" : "FilledStacks" },{ "name" : "Dane", "alias" : "FilledStacks" }]';

    List uniqueList = new List(); 列出 parsedJson = json.decode(jsonData);

    for (var jsonElement in parsedJson) {

    bool isPresent = false;
    for (var uniqueEle in uniqueList) {
      if (uniqueEle['name'] == jsonElement['name']) isPresent = true;
    }
    if (!isPresent) uniqueList.add(jsonElement);
    

    }

    print('$uniqueList');

    打印('');

    print('${parsedJson.runtimeType} : $parsedJson');

    }

    【讨论】:

      猜你喜欢
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2013-06-09
      • 2016-01-05
      • 2014-08-23
      • 2021-07-24
      相关资源
      最近更新 更多