gpt4 book ai didi

java - 将Java对象转换为与D3的圆形包装图兼容的JSON

转载 作者:行者123 更新时间:2023-12-02 10:43:06 32 4
gpt4 key购买 nike

我有一个如下所示的java数据结构

public class Dir {
private Map<String, Dir> dirs = new HashMap<>();
private Map<String, GitFile> files = new HashMap<>();
private String name;
public Dir(String name){
this.name = name;
}
public String getName() {
return name;
}
}

这表示一个目录结构,其中每个目录可以有更多目录或文件。我也可以使用其他辅助方法正确填充它。

现在的挑战是将其转换为如下所示的格式

{
"name": "flare",
"children": [
{
"name": "something",
"children": [
{
"name": "some_other",
"children": null,
"size": 138
},
{
"name": "some_other_other",
"children": null,
"size": 305
}
],
"size": 443
}
]
}

为了实现这一点,我编写了以下方法

public JsonObject toJson(){
JsonObject object = new JsonObject();
return buildJson(object);
}

private JsonObject buildJson(JsonObject object){
System.out.println("generating json for "+this.name);
for(Map.Entry<String,Dir> entry:dirs.entrySet()){

Dir thisDir = entry.getValue();
object.addProperty("name",thisDir.name);
JsonArray element = (JsonArray) new Gson().toJsonTree( thisDir.files.values());
element.add(thisDir.toJson());
object.add("children",element);

}
System.out.println(object);
return object;
}

但这不知何故只返回我最后一个目录中的文件。任何想法都会非常有帮助

这就是我调用该方法的方式

def projectDir = new Dir("/")
projectDir.addFile(new GitFile("/dev/", "talk.txt", 21, 109210))
projectDir.addFile(new GitFile("/etc/", "talk.txt", 21, 109210))
projectDir.addFile(new GitFile("/Users/sapanparikh", "talk.txt", 21, 109210))
projectDir.addFile(new GitFile("/Users/sapanparikh/development", "main.java", 12, 1229))
projectDir.addFile(new GitFile("/Users/sapanparikh/development/cat", "cat.java", 90, 309210))
projectDir.addFile(new GitFile("/Users/savi/development/cat", "cat.java", 90, 309210))
JsonObject jsonObject = projectDir.toJson();

最后打印 JSON 对象将给出以下内容

{
"name": "Users",
"children": [{
"name": "sapanparikh",
"children": [{
"path": "/Users/sapanparikh",
"name": "talk.txt",
"changeFrequency": 21,
"loc": 109210
}, {
"name": "development",
"children": [{
"path": "/Users/sapanparikh/development",
"name": "main.java",
"changeFrequency": 12,
"loc": 1229
}, {
"name": "cat",
"children": [{
"path": "/Users/sapanparikh/development/cat",
"name": "cat.java",
"changeFrequency": 90,
"loc": 309210
}, {}]
}]
}]
}]
}

这是整个Java程序的要点 https://gist.github.com/sapanparikh18/5365ec4d5a3d82b7c2f9489af34d7840

最佳答案

我认为 buildJson 中没有处理中间文件夹的文件。相反,我会使用递归方法调用来完成此操作。这对我有用:

private JsonObject buildJson(JsonObject object){
System.out.println("generating json for "+this.name);
object.addProperty("name", this.name);
JsonArray allChildren = new JsonArray();
JsonObject aChild;
for(Map.Entry<String,Dir> entry:dirs.entrySet()){
aChild=new JsonObject();
Dir thisDir = entry.getValue();
thisDir.buildJson(aChild);
allChildren.add(aChild);
}
JsonArray thisFiles = (JsonArray) new Gson().toJsonTree( this.files.values());
allChildren.addAll(thisFiles);
if (allChildren.size() == 0)
allChildren=null;
object.add("children",allChildren);
System.out.println(object);
return object;
}

关于java - 将Java对象转换为与D3的圆形包装图兼容的JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52790865/

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