insomniac insomniac - 4 months ago 8
JSON Question

Mapping storage structure to JSON format in Android

I am doing a project in which the user can grant file system access to third parties over the internet,I written the following code to map all the files in the storage:

private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {

inFiles.addAll(getListFiles(file));
} else {

inFiles.add(file);

}
}
return inFiles;
}


And i am using it as

JSONArray jarray=new JSONArray(getListFiles(Environment.getExternalStorageDirectory()));


And the problem which i am facing is that I get the whole file paths in the json object. but what i require is an ordered Json structure of the files and directories(Only with name and sub directories), Excluding the whole file path. I have tried to use models to map files and directories like below

public class BasiDir {
List<BasiDir> subDir;
String name;
List<FileInside> files;
/**
* @return the subDir
*/
public List<BasiDir> getSubDir() {
return subDir;
}
/**
* @param subDir the subDir to set
*/
public void setSubDir(List<BasiDir> subDir) {
this.subDir = subDir;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the files
*/
public List<FileInside> getFiles() {
return files;
}
/**
* @param files the files to set
*/
public void setFiles(List<FileInside> files) {
this.files = files;
}
}



public class FileInside {
String filepath,filename;

/**
* @return the filename
*/
public String getFilename() {
return filename;
}

/**
* @param filename the filename to set
*/
public void setFilename(String filename) {
this.filename = filename;
}

}


But i get confused most of the time where to use them,can anybody point me in the right direction by which i could accomplish this task,any link to third party libraries are also welcomed

EDIT

The format i require is as follows:
(For a directory with two files and a sub directory)

{
"root":[
{
"filename":"a.txt"
},
{
"filename":"b.txt"
},
{
"dirA":[
{
"filename":"c.txt"
},
{
"filename":"d.txt"
}
]
}
]
}

Answer

Thank you greenapps for asking me for the desired json,it was only then that i was able to eliminate the confusion and tweak the function like this:-

private Dir getListFiles(File parentDir) {
        Dir d=new Dir();
        d.setDirName(parentDir.getName());
        ArrayList<Dir> inDirs = new ArrayList<Dir>();
        ArrayList<MyProject.file.transfer.File> inFiles = new ArrayList<MyProject.file.transfer.File>();
        File[] files = parentDir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                inDirs.add(getListFiles(file));

            } else {
                MyProject.file.transfer.File f=new MyProject.file.transfer.File();
                f.setFilename(file.getName());
                f.setPath(file.getAbsolutePath());
                    inFiles.add(f);

            }
        }

        d.setFiles(inFiles);
        d.setSubDirs(inDirs);
        return d;
    }

And the Model classes:-

1.Dir.java

public class Dir {
String DirName;
List<File> Files;
List<Dir> SubDirs;
/**
 * @return the dirName
 */
public String getDirName() {
    return DirName;
}
/**
 * @param dirName the dirName to set
 */
public void setDirName(String dirName) {
    DirName = dirName;
}
/**
 * @return the files
 */
public List<File> getFiles() {
    return Files;
}
/**
 * @param files the files to set
 */
public void setFiles(List<File> files) {
    Files = files;
}
/**
 * @return the subDirs
 */
public List<Dir> getSubDirs() {
    return SubDirs;
}
/**
 * @param subDirs the subDirs to set
 */
public void setSubDirs(List<Dir> subDirs) {
    SubDirs = subDirs;
}

}

2.File.java

public class File {
    String filename;
    String path;
    /**
     * @return the filename
     */
    public String getFilename() {
        return filename;
    }
    /**
     * @param filename the filename to set
     */
    public void setFilename(String filename) {
        this.filename = filename;
    }
    /**
     * @return the path
     */
    public String getPath() {
        return path;
    }
    /**
     * @param path the path to set
     */
    public void setPath(String path) {
        this.path = path;
    }


}

And I use it with Gson like:

String json=new Gson().toJson(getListFiles(new File(Environment.getExternalStorageDirectory())),Dir.class);

I would have deleted it,But I'm just keeping the question here Hoping this may help someone else in the future