java - 从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 适配器中
解决方案
首先,将您的 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"}]
愿这能达到你的目的。
推荐阅读
- alert - zabbix 5.X 和 MS 团队集成
- javascript - 使用 Jquery 填充 AngularJS 表单
- node.js - node.js 说找不到 sqlite3 模块
- git - 克隆 GIT 存储库时,Visual Studio“自动”检查出错误的分支
- android - Google Play 商店:由于 Intent 重定向漏洞,Android 应用被拒绝
- macos - 如何在 Mac 上使用 RegExs 编辑 TTS 发音
- visual-studio-code - 当我在 vscode 中运行 python 时,它会在我的浏览器中打开一个选项卡
- flask - Flask-SocketIO 中的 RAM 使用量不断增加
- spring - com.netflix.zuul.exception.ZuulException:转发错误异常
- angular - 拒绝执行谷歌点击登录功能的脚本错误