如何将文件夹和子文件夹列表转换为JSOn FOrmat

时间:2015-06-24 11:45:30

标签: java json

我想在KendoDrop Down上填充子文件夹名称的文件夹名称。所以我想以JSOn格式转换文件夹目录我该怎么做?

public class FolderPath {

    public static void main(String[] args) throws IOException {
        File currentDir = new File("Folder URL "); // current directory
        displayDirectoryContents(currentDir);

    }
    public static void displayDirectoryContents(File dir) {

        StringBuilder sb1 = new StringBuilder("[");
        try {

            File[] files = dir.listFiles();

            for (File file : files) {
                if (file.isDirectory()) {
                    sb1 = sb1.append("{\"JSONKEY\":\"" + file.getCanonicalPath() + "\"},");
                    String str = file.getCanonicalPath();
                    displayDirectoryContents(file);

                } else {
                }

            }
            sb1.deleteCharAt(sb1.length() - 1);
            sb1 = sb1.append("]");
            System.out.println("s2==>" + sb1);

        } catch (IOException e) {
            e.printStackTrace();
        }   
    }
}

这里我没有完全转向JSOn请帮助

2 个答案:

答案 0 :(得分:0)

您正在每次迭代时创建一个StringBuilder对象。这就是你的连接不起作用的原因。

考虑你C:\test的内容由3个目录组成:

c:\test
  |
  +--css
  |   +--less
  +--js

以下代码返回:

 [{"JSONKEY":"C:\test\css"},
  {"JSONKEY":"C:\test\css\less"},
  {"JSONKEY":"C:\test\js"}]


import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

public class FolderPath {

    private static FileFilter onlyDirectories = new FileFilter() {
        @Override
        public boolean accept(File file) {
            return file.isDirectory();
        }
    };

    public static void main(String[] args) {
        File currentDir = new File("C:\\test"); // current directory
        displayDirectoryContents(currentDir);
    }

    public static void displayDirectoryContents(File dir) {
        StringBuilder sb1 = new StringBuilder("[");
        doDisplayDirectoryContents(dir, sb1);
        if (sb1.length() > 1) {
            sb1.deleteCharAt(sb1.length() - 1);
        }
        sb1.append("]");
        System.out.println(sb1);
    }

    private static void doDisplayDirectoryContents(File dir, StringBuilder sb1) {
        File[] files = dir.listFiles(onlyDirectories);

        if (files != null) {
            for (File file : files) {
                try {
                    sb1.append("{\"JSONKEY\":\"" + file.getCanonicalPath() + "\"},");
                    doDisplayDirectoryContents(file, sb1);
                } catch (IOException e) {
                    e.printStackTrace();
                }   
            }
        }
    }
}

答案 1 :(得分:-2)

public List<Object> getDirectoryContents(String path) throws IOException {
    File directory = new File(path);
    File[] files;
enter code here    FileFilter fileFilter = file -> file.isDirectory() || file.isFile();
    files = directory.listFiles(fileFilter);
    List<Object> directoryContent = new ArrayList<>();
    if(files != null) {
        for (int i = 0; i < files.length; i++) {
            File filename = files[i];
            String folderPath[] =filename.toString().split("/");
            if(files[i].isDirectory()) {
                Folder folder = new Folder();
                folder.setName(folderPath[folderPath.length - 1]);
                folder.setType("folder");
                folder.setChildren(mapper.readTree(mapper.writeValueAsString(getDirectoryContents(path + "/" + folder.getName()))));
                directoryContent.add(folder);
            }
            else{
                Files file = new Files();
                file.setName(folderPath[folderPath.length - 1]);
                file.setType("file");
                directoryContent.add(file);
            }
        }
    }
    return directoryContent;
}

公共类文件{

private String name;
private String type = "file";

}

公共类文件夹{

private String name;
private String type = "folder";
private JsonNode children;

}