项目中需要解析一个200M的json目录文件,然后根据需要读取field
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class ReadJsonFile { private ObjectMapper mapper = new ObjectMapper(); public JsonNode readAsTree(Path jsonPath) throws Exception, IOException { JsonNode jsonNode = mapper.readTree(Files.newInputStream(jsonPath)); return jsonNode; } }
从路径为jsonPath的文件中读取了json字符串,然后转换成JsonNode
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SequenceWriter; public class WriteJsonFile { private ObjectMapper mapper = new ObjectMapper(); public void writeJsonFile(JsonNode rootNode, Path outputPath) throws IOException { SequenceWriter writer = mapper.writerWithDefaultPrettyPrinter() .writeValues(Files.newOutputStream(outputPath)); writer.write(rootNode); writer.close(); } }
把JsonNode对象转成json字符串写入到某个文件中,也可以是其他类型的对象
public void handleElements(JsonNode arrayNode) { Iterator<JsonNode> realElements = arrayNode.elements(); while (realElements.hasNext()) { JsonNode realElement = realElements.next(); System.out.println(realElement.toString()); } }
如果是一个JsonNode数组,比如这里的arrayNode,使用arrayNode.elements();读取数组中每个node
如果不是JsonNode数组arrayNode.elements();返回arrayNode的values
public void traverseJson(JsonNode jsonNode) throws IOException { Iterator<String> fieldNames = jsonNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); System.out.println(fieldName); } }
jsonNode的fieldNames就是该jsonNode的所有的key值
public void traverseJson(JsonNode jsonNode) { Iterator<Entry<String, JsonNode>> jsonNodes = jsonNode.fields(); while (jsonNodes.hasNext()) { Entry<String, JsonNode> node = jsonNodes.next(); System.out.println(node.getKey()); System.out.println(node.getValue().toString()); } }
类似Map的Entry方式遍历某个JsonNode的key和value
value可能是字符串也可能是子jsonNode,但如果value是jsonNode数组的话,是无法读取的
public void traverseJson(JsonNode jsonNode) { List<JsonNode> jsonNodes = jsonNode.findParents("fieldName"); System.out.println(jsonNodes.size()); }
取出所有key值为fieldName的JsonNode的List
public void traverseJson(JsonNode jsonNode) { List<JsonNode> jsonNodes = jsonNode.findValues("fieldName"); System.out.println(jsonNodes.size()); }
取出所有key值为fieldName对应的value,这些values就是JsonNode的List
但是如果value中包含子jsonNode并且子jsonNode的key值也为fieldName,是无法捕获到并加入list的
public void traverseJson(JsonNode jsonNode) { JsonNode node1 = jsonNode.findValue("fieldName"); JsonNode node2 = jsonNode.findPath("fieldName"); }
取出key值为fieldName的一个value,匹配到一个就停止搜索,并且返回value
node1和node2的区别在于,如果没有匹配到任何key值为fieldName,node1为null,node2为空JsonNode
类似于
List<String> node1 = null; List<String> node2 = new ArrayList<>();
public void traverseJson(JsonNode jsonNode) { //{"name" : "xiaoLi"} String name = jsonNode.findValue("name").asText(); System.out.println(name); }
如果value为String,可以这样读取,其他类型可以jsonNode.findValue("name").asInt();
public void traverseJson(JsonNode jsonNode) { jsonNode.forEach((JsonNode node)->{ System.out.println(node.toString()); }); }
如果是JDK1.8的话,可以这样遍历JsonNode的子节点
以上