gpt4 book ai didi

java - 如何通过 Jackson 2 JsonNode 树遍历 JSON?

转载 作者:行者123 更新时间:2023-12-02 04:29:52 35 4
gpt4 key购买 nike

这有点让人沮丧,因为我找不到这个问题的任何答案,这让情况变得更糟。所以我就在这里回答这个问题。

我发现最难理解的是 JsonNode 没有任何 getName() 或类似的方法,这是我所期望的,因为 JSON 是一个 name:value数据类型。尽管我在解决这个解决方案后意识到数组不是名称:值。

请参阅下面的答案。

最佳答案

package treeWalker;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;

public class TreeWalker
{
public JsonNode convertJSONToNode(String json) throws JsonProcessingException, IOException
{
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);

return jsonNode;
}

public void walkTree(JsonNode root)
{
walker(null, root);
}

private void walker(String nodename, JsonNode node)
{
String nameToPrint = nodename != null ? nodename : "must_be_root";
System.out.println("walker - node name: " + nameToPrint);
if (node.isObject())
{
Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();

ArrayList<Map.Entry<String, JsonNode>> nodesList = Lists.newArrayList(iterator);
System.out.println("Walk Tree - root:" + node + ", elements keys:" + nodesList);
for (Map.Entry<String, JsonNode> nodEntry : nodesList)
{
String name = nodEntry.getKey();
JsonNode newNode = nodEntry.getValue();

// System.out.println(" entry - key: " + name + ", value:" + node);
walker(name, newNode);
}
}
else if (node.isArray())
{
Iterator<JsonNode> arrayItemsIterator = node.elements();
ArrayList<JsonNode> arrayItemsList = Lists.newArrayList(arrayItemsIterator);
for (JsonNode arrayNode : arrayItemsList)
{
walker("array item", arrayNode);
}
}
else
{
if (node.isValueNode())
{
System.out.println(" valueNode: " + node.asText());
}
else
{
System.out.println(" node some other type");
}
}
}
}

还有一个要练习的单元测试(没有断言!抱歉)。

package treeWalker;

import java.io.IOException;

import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;


public class TreeWalkerTest
{
TreeWalker treeWalker = new TreeWalker();

private String getJSON()
{
String json = "{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":[1,2,3]}";
return json;
}

@Test
public void testConvertJSONToNode() throws JsonProcessingException, IOException
{
String json = getJSON();

JsonNode jNode = treeWalker.convertJSONToNode(json);

System.out.println("jnode:" + jNode);

}

@Test
public void testWalkTree() throws JsonProcessingException, IOException
{
JsonNode jNode = treeWalker.convertJSONToNode(getJSON());

treeWalker.walkTree(jNode);
}
}

哦,还有 build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
mavenCentral()
}

dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
compile 'com.fasterxml.jackson.core:jackson-core:2.4.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.3'
compile 'com.google.guava:guava:18.0'

testCompile "junit:junit:4.11"
}

关于java - 如何通过 Jackson 2 JsonNode 树遍历 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26526030/

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