代码之家  ›  专栏  ›  技术社区  ›  Sap

将Java对象转换为与D3的循环打包图兼容的JSON

  •  0
  • Sap  · 技术社区  · 6 年前

    我有一个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();
    

    {
        "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 回复  |  直到 6 年前
        1
  •  1
  •   Maksim    6 年前

    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;
    }