gpt4 book ai didi

java - Java中的Json解析仅通过提供一次键从数组中获取值?

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

例如,以下 JSON

[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
]
<小时/>

当我们处理大量数组时,如何才能只获取一次键。就像Give id一样,它一次又一次地给出所有数组的键。我只想获取 id 一次,当用户输入 id 时,它会给出整个数组中 id 的值。

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ValueNode;
import java.io.File;
import java.io.IOException;
import java.util.*;

public class Main {
public static void main(String[] args) {
File file=new File("src/data.json");
ObjectMapper mapper=new ObjectMapper();
Scanner scanner=new Scanner(System.in);
try {

LinkedHashMap<String,String> map= new LinkedHashMap<String, String>();
JsonNode node =mapper.readTree(file);
getKeys("",node, map);
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() );
}
System.out.println();
while (true) {
System.out.println(map.get(scanner.next()));

}

} catch (IOException e) {
e.printStackTrace();
}
}
public static void getKeys(String currentpath,JsonNode node,LinkedHashMap map){
if(node.isObject()){
ObjectNode objectNode=(ObjectNode) node;
Iterator<Map.Entry<String, JsonNode>> it=objectNode.fields();
String prefix=currentpath.isEmpty()?"":currentpath+".";
while (it.hasNext()){
SortedMap.Entry<String,JsonNode> iter=it.next();
getKeys(prefix+iter.getKey(),iter.getValue(),map);
}
}else if (node.isArray()){
ArrayNode arrayNode=(ArrayNode) node;
for(int i=0; i<arrayNode.size(); i++){
getKeys(currentpath+i,arrayNode.get(i),map);
}
}
else if(node.isValueNode()) {
ValueNode valueNode=(ValueNode) node;
map.put(currentpath,valueNode.asText());
}
}
}
<小时/>

下面是代码结果和预期结果

key:0.address.city
Key:0.address.geo.lng
Key:0.name
Key:0.username

key:1.address.city
Key:1.address.geo.lng
Key:1.name
Key:1.username

output should like this with out index of array

name
username
address.city
address.geo.lng

Graham
Bret
Gwenborough
81.1496

aham
ret
borough
80.1496

------------------

最佳答案

您可以创建一个Map<String, List<String>>为了将一个键映射到它的所有值。例如。替换

else if (node.isValueNode()) {
...
}

else if (node.isValueNode()) {
ValueNode valueNode = (ValueNode) node;
final List<String> values = map.getOrDefault(currentpath, new ArrayList<>());
values.add(valueNode.asText());
map.put(currentpath, values);
}

如果您现在输入 ID,它将显示列表中的所有值。

关于java - Java中的Json解析仅通过提供一次键从数组中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61496131/

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