java 生成jsonPath树
2022/2/14 20:11:39
本文主要是介绍java 生成jsonPath树,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public static void main(String[] args) throws Exception { generateJsonPath(); } public static void generateJsonPath() throws Exception { Map<String, Object> data = new HashMap<>(); data.put("list", new Stu[]{new Stu("xxxx", System.currentTimeMillis())}); data.put("obj", new Stu("cc", System.currentTimeMillis())); ObjectMapper mapper = new ObjectMapper(); JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder() .setSpecVersion(SpecVersion.DRAFT_04) .setAdditionalPropertiesPolicy(AdditionalPropertiesPolicies.noOp()) .setRequiredPolicy(RequiredPolicies.noOp()) .build(); // System.out.println(JsonUtils.toJSONString(data)); JsonNode objectNode = inferrer.inferForSample(mapper.readTree(JsonUtils.toJSONString(data))); System.out.println(objectNode.toString()); List<PathInfo> pathInfoList = new ArrayList<>(); Map<String, PathInfo> pathInfoMap = new HashMap<>(); PathInfo rootNode = new PathInfo(UUID.randomUUID().toString(), "-1", "$", "$", objectNode.get("type").asText()); if ("array".equals(rootNode.getType())) { rootNode.setPath(rootNode.getPath() + "[]"); } pathInfoList.add(rootNode); pathInfoMap.put(rootNode.getId(), rootNode); deepNode(pathInfoList, pathInfoMap, "array".equals(rootNode.getType()) ? objectNode.get("items").get("properties") : objectNode.get("properties"), rootNode.getId()); System.out.println(JsonUtils.toJSONString(pathInfoMap)); Map<String, List<PathInfo>> map = pathInfoList.stream().collect(Collectors.groupingBy(PathInfo::getParentId)); pathInfoList.forEach(e -> e.setChild(map.getOrDefault(e.getId(), new ArrayList<>()))); System.out.println(JsonUtils.toJSONString(rootNode)); } public static void deepNode(List<PathInfo> pathInfoList, Map<String, PathInfo> pathInfoMap, JsonNode jsonNode, String parentId) { Iterator<Map.Entry<String, JsonNode>> iterator = jsonNode.fields(); while (iterator.hasNext()) { Map.Entry<String, JsonNode> nodeEntry = iterator.next(); JsonNode node = nodeEntry.getValue(); String prop = nodeEntry.getKey(); System.out.println(nodeEntry); PathInfo pathNode = new PathInfo(UUID.randomUUID().toString(), parentId, prop, pathInfoMap.get(parentId).getPath() + "." + prop, node.get("type").asText()); if ("array".equals(pathNode.getType())) { pathNode.setPath(pathNode.getPath() + "[]"); } pathInfoList.add(pathNode); pathInfoMap.put(pathNode.getId(), pathNode); if (pathNode.getType().equals("object")) { deepNode(pathInfoList, pathInfoMap, node.get("properties"), pathNode.getId()); } if (pathNode.getType().equals("array")) { deepNode(pathInfoList, pathInfoMap, node.get("items").get("properties"), pathNode.getId()); } } } @Data public static class PathInfo { private String id; private String parentId; private String propName; private String path; private String alias; private String type; private List<PathInfo> child; public PathInfo() { } public PathInfo(String id, String parentId, String propName, String path, String type) { this.id = id; this.parentId = parentId; this.propName = propName; this.path = path; this.type = type; } } @Data public static class Stu { private String name; private Long id; public Stu(String name) { this.name = name; } public Stu(String name, Long id) { this.name = name; this.id = id; } public Stu() { } public String getName() { return name; } public void setName(String name) { this.name = name; } }
执行结果
直接上测试代码 依赖
<dependency> <groupId>com.github.saasquatch</groupId> <artifactId>json-schema-inferrer</artifactId> <version>0.1.4</version> </dependency> 有问题请指正
这篇关于java 生成jsonPath树的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南