【问题标题】:Adobe Air HTML recursive file walk to JSONAdobe Air HTML 递归文件遍历到 JSON
【发布时间】:2014-04-13 20:00:09
【问题描述】:

我尝试使用 Adob​​e 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>

【问题讨论】:

    标签: javascript json recursion air directory


    【解决方案1】:

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

    function select_child(child) 
      { 
      return dirTree(child.nativePath);
      }
    
    info.children = currentFile.getDirectoryListing().map(select_child);
    

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 2019-08-16
      • 2016-08-25
      • 2011-03-21
      • 2018-07-24
      • 2016-10-28
      相关资源
      最近更新 更多