gpt4 book ai didi

java - 将 json 数据简化为避免特定 json 键内重复值的形式

转载 作者:行者123 更新时间:2023-12-01 09:09:42 25 4
gpt4 key购买 nike

任何人都可以建议我一种方法,根据给定的 json 中存在的重复值将其简化为更简单的 json 吗?例如:如果以下是我的 json 数据,

{
"result": [{
"LOCATION": [
"US",
"United States",
"United States",
"Honolulu",
"Hawaii",
"Chicago",
"13th District",
"United States"
],
"ORGANIZATION": [
"Columbia University",
"Harvard Law School",
"Harvard Law School",
"University of Chicago Law School",
"Illinois Senate",
"House of Representatives"
]
}]
}

Then i need the output after json reduction as ,

{
"result": [{
"LOCATION": [
"US",
"United States (3)",
"Honolulu",
"Hawaii",
"Chicago",
"13th District"
],
"ORGANIZATION": [
"Columbia University",
"Harvard Law School (2)",
"University of Chicago Law School",
"Illinois Senate",
"House of Representatives"
]
}]
}

即每个重复值必须替换为单个相同值以及 json 中相同值的出现次数。有没有任何库可以这样做?

提前致谢。

最佳答案

请发布您的代码并更清楚地给出您的要求。是否有一些 POJO 可用于此 JSON?直接没有库可以进入数组并根据您的要求更改它,因此您必须编写自己的自定义解析器。

我通过假设三件事创建了一个示例代码片段:

您的结构将保持不变,即:

  1. 外部元素“结果”,它是一个数组
  2. 每个数组元素本身又是一个对象
  3. 在该对象内部,我们有许多需要优化的数组

示例:

{
"result": [//ASSUMPTION [1]

{ //ASSUMPTION [2]
"LOCATION": [ //ASSUMPTION [3]
"US",
"United States",
"United States",
"Honolulu",
"Hawaii",
"Chicago",
"13th District",
"United States"
],
"ORGANIZATION": [ //ASSUMPTION [3]
"Columbia University",
"Harvard Law School",
"Harvard Law School",
"University of Chicago Law School",
"Illinois Senate",
"House of Representatives"
]
},
{ //ASSUMPTION [2]
"LOCATION": [ //ASSUMPTION [3]
"United States",
"United States"
],
"ORGANIZATION": [ //ASSUMPTION [3]
"Harvard Law School",
"Harvard Law School"
]
}
]
}

将其传递给代码后,我得到了这个结果:

[{
"LOCATION": ["US", "Honolulu", "Hawaii", "Chicago", "13th District", "United States (3)"],
"ORGANIZATION": ["Columbia University", "University of Chicago Law School", "Illinois Senate", "House of Representatives", "Harvard Law School (2)"]
}, {
"LOCATION": ["United States (2)"],
"ORGANIZATION": ["Harvard Law School (2)"]
}]

这是非常原始的代码,没有任何优化,但你会明白的。

代码片段:

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = new HashMap<String, Object>();
// convert JSON string to Map
map = mapper.readValue(new File("c://drive//test.json"), new TypeReference<Map<String, Object>>() {
});
System.out.println("map " + map);

// Value of result element i.e array
List<Map<String, Object>> result = (List<Map<String, Object>>) map.get("result");
System.out.println("result " + result);
// Traverse over array, Each element inside is one more object (ASSUMPTION 2)
for (Map<String, Object> resultMap : result) {
List<String> traversed = new ArrayList<>();
// Traverse over each object
for (Entry<String, Object> entry : resultMap.entrySet()) {
// Each object contains a list
List<String> insideMostArray = (List<String>) entry.getValue();
// Entries to remove
Map<String, Integer> removedEntry = new HashMap<>();
// Inside most values
for (String insideMostArrayTemp : insideMostArray) {
if (traversed.contains(insideMostArrayTemp)) {
removedEntry.put(insideMostArrayTemp, removedEntry.get(insideMostArrayTemp) == null ? 2
: (removedEntry.get(insideMostArrayTemp) + 1));
} else {
traversed.add(insideMostArrayTemp);
}
}
//Remove all duplicate entries and replace with 1 entry
for (Entry<String, Integer> entriesToRemove : removedEntry.entrySet()) {
insideMostArray.removeAll(Collections.singleton(entriesToRemove.getKey()));
insideMostArray.add(entriesToRemove.getKey() + " (" + entriesToRemove.getValue() + ")");
}
}
}
String optimizedJson = mapper.writeValueAsString(result);
}

关于java - 将 json 数据简化为避免特定 json 键内重复值的形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40996304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com