Wrote a little program to solve your problem.
A recursive Type Entity that has 2 attributes, "name" of type String and "children" of Type List. Since this is the structure of your peaceable JSON. The JSON then can be easily parsed using Jackson API in one line
List<Entity> clas = mapper.readValue(json,
mapper.getTypeFactory().constructCollectionType(List.class, Entity.class));
After successfully creating the object, all we need to do is traverse it recursively through the hierarchy and in every step return the name attribute with some extra text that gives the result a valid JSON form. Code below.
Main.java
import java.io.IOException;
import java.util.List;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class Main {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String json = "[{\"name\":\"com\",\"children\":[{\"name\":\"project\",\"children\":[{\"name\":\"server\"},{\"name\":\"client\",\"children\":[{\"name\":\"util\"}]}]}]}]";
ObjectMapper mapper = new ObjectMapper();
List<Entity> clas = mapper.readValue(json,
mapper.getTypeFactory().constructCollectionType(List.class, Entity.class));
String names = getChildren(clas);
String result = "[" + names.substring(0, names.length() - 1) + "]";
System.out.println(result);
}
private static String getChildren(List<Entity> clas) {
String names = "";
for (Entity class1 : clas) {
if (class1.getChildren() == null) {
names += "{name : \"" + class1.getName() + "\"},";
if (clas.indexOf(class1) == (clas.size() - 1)) {
return names;
}
continue;
}
names += "{name : \"" + class1.getName() + "\"},";
names += getChildren(class1.getChildren());
}
return names;
}
}
Entity.java
import java.util.List;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Entity {
String name;
List<Entity> children;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Entity> getChildren() {
return children;
}
public void setChildren(List<Entity> children) {
this.children = children;
}}