Adobe Air HTML递归文件遍历JSON

时间:2014-03-10 15:48:53

标签: javascript json recursion air directory

我尝试使用Adobe AIR HTML创建目录列表。输出应该是JSON文档。

作为模板,我使用了这个node.js代码:https://stackoverflow.com/a/11194896/799692

文件行走有效。但是JSON对象只包含最后一个文件夹。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Adobe Air directory listening to JSON</title>
    <script type="text/javascript" src="AIRAliases.js"></script>  <!-- adobe air framework -->  
    <script type="text/javascript" src="jquery-2.0.3.min.js"></script>
</head>
<body>

<script type="text/javascript">

var completePath = air.File.applicationDirectory.resolvePath("data").nativePath;

window.onload = function()
{   

    air.trace(JSON.stringify(dirTree(completePath), null, 4));

}



function dirTree(filename) 
{

    var currentFile = new air.File(filename);   
    var temp_path = currentFile.nativePath;                 


    info = {
        path: temp_path,
        name: currentFile.name
    };

    if (currentFile.isDirectory)
    {   
        if (currentFile.name !="." && currentFile.name !="..")
            {
            info.type = "folder";
            air.trace(Object.size(info));
            info.children = currentFile.getDirectoryListing().map(function(child) { 
                    return dirTree(child.nativePath);
            });
            }
        }
        else 
        {
            info.type = "file";         

        }   
    return info;                
} //end fn dirTree


// simple helper for object size
 Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};


</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

用函数指针替换map匿名函数:

function select_child(child) 
  { 
  return dirTree(child.nativePath);
  }

info.children = currentFile.getDirectoryListing().map(select_child);