Java生成嵌套对象JSON文件及POM依赖版本提取格式调整需求
Hey there! Let's tackle your two Java JSON-related tasks step by step. I'll use Jackson (the most popular JSON library for Java) and Maven's own model parser to get this done cleanly.
To create JSON with nested objects, we'll use Jackson's ObjectMapper to serialize Java objects directly into structured JSON. First, add the Jackson dependency to your pom.xml:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency>
Next, define nested Java classes that match your desired JSON structure. For example, if you want to group components by their groupId:
// Nested class for individual component details class ComponentVersion { private String artifactId; private String version; // Constructor, getters, and setters public ComponentVersion(String artifactId, String version) { this.artifactId = artifactId; this.version = version; } public String getArtifactId() { return artifactId; } public void setArtifactId(String artifactId) { this.artifactId = artifactId; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } } // Parent class for grouping components by groupId class ComponentGroup { private String groupId; private List<ComponentVersion> components; public ComponentGroup(String groupId, List<ComponentVersion> components) { this.groupId = groupId; this.components = components; } public String getGroupId() { return groupId; } public void setGroupId(String groupId) { this.groupId = groupId; } public List<ComponentVersion> getComponents() { return components; } public void setComponents(List<ComponentVersion> components) { this.components = components; } } // Top-level class to wrap all groups class ComponentReport { private List<ComponentGroup> componentGroups; public ComponentReport(List<ComponentGroup> componentGroups) { this.componentGroups = componentGroups; } public List<ComponentGroup> getComponentGroups() { return componentGroups; } public void setComponentGroups(List<ComponentGroup> componentGroups) { this.componentGroups = componentGroups; } }
Now, serialize these objects to a JSON file:
import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class NestedJsonGenerator { public static void main(String[] args) { // Build sample nested data List<ComponentVersion> springComponents = new ArrayList<>(); springComponents.add(new ComponentVersion("spring-core", "5.3.20")); springComponents.add(new ComponentVersion("spring-web", "5.3.20")); List<ComponentVersion> jacksonComponents = new ArrayList<>(); jacksonComponents.add(new ComponentVersion("jackson-databind", "2.13.3")); List<ComponentGroup> groups = new ArrayList<>(); groups.add(new ComponentGroup("org.springframework", springComponents)); groups.add(new ComponentGroup("com.fasterxml.jackson.core", jacksonComponents)); ComponentReport report = new ComponentReport(groups); // Write to JSON file with pretty formatting ObjectMapper objectMapper = new ObjectMapper(); try { objectMapper.writerWithDefaultPrettyPrinter() .writeValue(new File("nested-components.json"), report); System.out.println("Nested JSON file generated successfully!"); } catch (IOException e) { e.printStackTrace(); } } }
The output JSON will look like this (properly nested):
{ "componentGroups" : [ { "groupId" : "org.springframework", "components" : [ { "artifactId" : "spring-core", "version" : "5.3.20" }, { "artifactId" : "spring-web", "version" : "5.3.20" } ] }, { "groupId" : "com.fasterxml.jackson.core", "components" : [ { "artifactId" : "jackson-databind", "version" : "2.13.3" } ] } ] }
To parse a POM file and extract dependencies, we'll use Maven's official maven-model library. Add these dependencies first:
<dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-model</artifactId> <version>3.8.6</version> </dependency> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> <version>3.4.2</version> </dependency>
Below is code that reads a POM, extracts all dependencies, converts them to your target nested format, and writes to JSON:
import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import java.io.FileReader; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class PomToJsonConverter { public static void main(String[] args) { MavenXpp3Reader pomReader = new MavenXpp3Reader(); try { // Read and parse the POM file Model pomModel = pomReader.read(new FileReader("pom.xml")); List<org.apache.maven.model.Dependency> dependencies = pomModel.getDependencies(); // Group dependencies by groupId to build nested structure Map<String, List<ComponentVersion>> groupMap = new HashMap<>(); for (org.apache.maven.model.Dependency dep : dependencies) { String groupId = dep.getGroupId(); ComponentVersion component = new ComponentVersion(dep.getArtifactId(), dep.getVersion()); // Add to group, create list if it doesn't exist groupMap.computeIfAbsent(groupId, k -> new ArrayList<>()).add(component); } // Convert map to ComponentGroup list List<ComponentGroup> componentGroups = new ArrayList<>(); for (Map.Entry<String, List<ComponentVersion>> entry : groupMap.entrySet()) { componentGroups.add(new ComponentGroup(entry.getKey(), entry.getValue())); } // Write to target JSON file ComponentReport report = new ComponentReport(componentGroups); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writerWithDefaultPrettyPrinter() .writeValue(new File("pom-components.json"), report); System.out.println("POM components extracted and saved to JSON!"); } catch (Exception e) { e.printStackTrace(); } } }
If your target format is a flat list (instead of grouped by groupId), just adjust the code to create a list of Component objects with groupId, artifactId, and version fields, then serialize that list directly.
内容的提问来源于stack exchange,提问作者Kiran k

