首页 > 解决方案 > 从android中的json数组中删除重复值

问题描述

这是当前结果 这是需要结果[{"product_name":"13X25","ask_size":0,"product_id":"5","category_id":"1","quantity":"10","image":null,"description": "","product_pdf":null,"head":"MARSHAL","price":"22.00","user_quantity":"2"},{"product_name":"14X25","ask_size":0," product_id":"6","category_id":"1","quantity":"12","image":null,"description":"","product_pdf":null,"head":"MARSHAL", "price":"23.00","user_quantity":"1"},{"product_name":"14X25 C","ask_size":0,"product_id":"2","category_id":"1","quantity":"23","image":null,"description":"","product_pdf":null,"head":"KANGARO","price" :"22.00","user_quantity":"0"},{"product_name":"17X25 C","ask_size":0,"product_id":"1","category_id":"1","quantity": "18","image":null,"description":"","product_pdf":null,"head":"HOKO","price":"12.00","user_quantity":"0"}]"KANGARO","price":"22.00","user_quantity":"0"},{"product_name":"17X25 C","ask_size":0,"product_id":"1","category_id":" 1","quantity":"18","image":null,"description":"","product_pdf":null,"head":"HOKO","price":"12.00","user_quantity": "0"}]"KANGARO","price":"22.00","user_quantity":"0"},{"product_name":"17X25 C","ask_size":0,"product_id":"1","category_id":" 1","quantity":"18","image":null,"description":"","product_pdf":null,"head":"HOKO","price":"12.00","user_quantity": "0"}]"HOKO","price":"12.00","user_quantity":"0"}]"HOKO","price":"12.00","user_quantity":"0"}]

这是我的 JSON 数组,如果特定键已经存在,我想在其中删除它

有一个名为“head”的密钥:“MARSHAL”,此密钥出现两次,但我只想获取一次。

如果它来了,那么它必须从其他 JSONObject 中自动删除,不仅是一个键

如果它们被重复,也可以是其他键,那么我只想要它们一次,然后将该 JSON 数据添加到列表中

我怎样才能为安卓做到这一点?

我想将此数据显示到 cardView 适配器中

标签: javaandroidjson

解决方案


首先,将您的 JSON 转换为 List<Map<String, Object>>。您可以使用 ObhectMapper 来执行此操作。

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class Test {
  private static ObjectMapper mapper = new ObjectMapper();

  public static void main(String[] args) throws IOException {
    List<Map<String, String>> listOfMap = createListMap();

    Map<String, List<String>> tempMap = new HashMap<>();
    listOfMap.forEach(
        map -> {
          Set<String> keySet = new HashSet<>(map.keySet());
          keySet.forEach(
              key -> {
                List<String> tempValue = tempMap.get(key);
                if (Objects.nonNull(tempValue) && tempValue.contains(map.get(key))) {
                  map.remove(key);
                } else {
                  tempMap.put(key, Arrays.asList(map.get(key)));
                }
              });
        });
    System.out.println(createListMap(listOfMap));
  }

  private static List<Map<String, String>> createListMap() throws IOException {
    File jsonFile = new File("src/main/resources/input.json");
    return mapper.readValue(jsonFile, new TypeReference<List<Map<String, String>>>() {});
  }

  private static String createListMap(List<Map<String, String>> listOfMap) throws IOException {
    return mapper.writeValueAsString(listOfMap);
  }
}

以下是您的输入 JSON 的输出。

[{"product_name":"13X25","ask_size":"0","product_id":"5","category_id":"1","quantity":"10","image":null,"description ":"","product_pdf":null,"head":"MARSHAL","price":"22.00","user_quantity":"2"},{"product_name":"14X25","product_id":" 6","quantity":"12","price":"23.00","user_quantity":"1"},{"product_name":"14X25 C","product_id":"2","quantity": "23","head":"KANGARO","price":"22.00","user_quantity":"0"},{"product_name":"17X25 C","product_id":"1","quantity":"18","head":"HOKO","price":"12.00"}]

愿这能达到你的目的。


推荐阅读