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

时间:2018-10-13 08:04:06

标签: java json recursion gson

我有一个如下所示的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

1 个答案:

答案 0 :(得分:1)

我认为buildJson中未处理中间文件夹的文件。相反,我将使用递归方法调用来做到这一点。这对我有用:

deploy:
  provider: lambda
  function_name: "lambda-test"
  region: "us-east-1"
  role: "arn:aws:iam::0123456789012:role/lambda_basic_execution"
  runtime: "nodejs4.3"
  handler_name: "handler"