解析json对象的文件路径字符串

时间:2018-04-07 05:30:26

标签: javascript jquery json

我有以下网址列表。如何将其转换为完整的json对象?

我的网址数组。

[
"path1/subpath1/file1.doc","path1/subpath1/file2.doc","path1/subpath2/file1.doc","path1/subpath2/file2.doc","path2/subpath1/file1.doc","path2/subpath1/file2.doc","path2/subpath2/file1.doc","path2/subpath2/file2.doc","path2/subpath2/additionalpath1/file1.doc"
]

我希望这个像json对象一样:

{
   "path1":{
      "subpath1":["file1.doc","file2.doc"],
      "subpath2":["file1.doc","file2.doc"]
   },
   "path2":{
      "subpath1":["file1.doc","file2.doc"],
      "subpath2":["file1.doc","file2.doc"],
      "additionalpath1":{
          "additionalpath1":["file1.doc"]
      }
   }
}

怎么做?

我用以下代码片段尝试了它。但是缺少一些文件夹对象。

如果您尝试使用此代码,则会发现test1和其他objects缺失:

<html>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<body>


<p id="demo"></p>

<script>
let paths = [ "admin/640954.jpg", "admin/test1/3m-nd.jpg", 
"admin/test1/Acct.png", "admin/test1/additional/111.gif", "admin/test1/additional/Aard.jpg", 
"dp/151292.jpg", "dp/151269.jpg", "dp/1515991.jpg" ];


function getMap(urls){
    var map = {};
    urls.forEach(function(url){
        var parts = url.split("/");
        makePath(map, parts);
    })
    return map;
}

function makePath(map,parts){
    var currentPath = map;
    for(var i = 0 ; i < parts.length - 1 ; i++ ){
            if(i == parts.length -2 ){
                currentPath[parts[i]]  = currentPath[parts[i]] || [];
                currentPath[parts[i]].push(parts[++i]);
            }else{
                currentPath[parts[i]] =  currentPath[parts[i]] || {};
                currentPath = currentPath[parts[i]];
            }
    }

}

document.getElementById("demo").innerHTML=JSON.stringify(getMap(paths));
</script>

</body>
</html>

5 个答案:

答案 0 :(得分:1)

我为你制作了半码。检查它是否对你有帮助。

您可以使用它并进行一些更改以实现目标。

<!DOCTYPE html>
<html>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<body>

<p>Click the button to display the array values after the split.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = '"path1/subpath1/file1.doc","path1/subpath1/file2.doc","path1/subpath2/file1.doc","path1/subpath2/file2.doc","path2/subpath1/file1.doc","path2/subpath1/file2.doc","path2/subpath2/file1.doc","path2/subpath2/file2.doc","path2/subpath2/additionalpath1/file1.doc"';
    var res = str.split(",");
    document.getElementById("demo").innerHTML = res[0];

    var finalresult = [];
    var innerarray = [];
    var outer = [];
    var outer1 = [];
    var inner =[];
    jQuery.each( res, function( i, val ) {
      res1 = val.split("/");
      jQuery.each( res1, function( i2, val1 ) 
      {
            if(i2 == 0 && !(outer.includes(val1)))
            {
                outer.push(val1);
            }
            else if(i2 == 1 && !(outer1.includes(val1)))
            {
                outer1.push(val1);
            }
            else if(!(inner.includes(val1)))
            {
                inner.push(val1);
            }
        console.log(outer);
      });

    });

}
</script>

</body>
</html>

答案 1 :(得分:0)

function filePathOject (arr) {
  const ret = {};
  arr.forEach((path) => {
    const dirs = path.split('/');
    const filename = dirs.pop();

    let dirObject = ret;
    dirs.forEach((dir, i) => {
      if (i === dirs.length - 1) {
        dirObject[dir] = dirObject[dir] || [];
        dirObject[dir].push(filename);
      } else {
        dirObject[dir] = dirObject[dir] || {};
      }
      dirObject = dirObject[dir];
    });
  });

  return ret;
}

答案 2 :(得分:0)

您可以使用.split("/")并迭代结果,在嵌套对象中创建属性:

&#13;
&#13;
let paths = [
  "path1/subpath1/file111.doc",
  "path1/subpath1/file112.doc",
  "path1/subpath2/file121.doc",
  "path1/subpath2/file122.doc",
  "path2/subpath1/file211.doc",
  "path2/subpath1/file212.doc",
  "path2/subpath2/file221.doc",
  "path2/subpath2/file222.doc",
  "path2/additionalpath3/additionalpath1/file2311.doc"
];

let treePath = {};
paths.forEach(path => {
  let levels = path.split("/");
  let file = levels.pop();

  let prevLevel = treePath;
  let prevProp = levels.shift();

  levels.forEach(prop => {
    prevLevel[prevProp] = prevLevel[prevProp] || {};
    prevLevel = prevLevel[prevProp];
    prevProp = prop;
  });

  prevLevel[prevProp] = (prevLevel[prevProp] || []).concat([file]);
});

console.log(treePath);
&#13;
&#13;
&#13;

或者:

&#13;
&#13;
let paths = [
  "path1/subpath1/file111.doc",
  "path1/subpath1/file112.doc",
  "path1/subpath2/file121.doc",
  "path1/subpath2/file122.doc",
  "path2/subpath1/file211.doc",
  "path2/subpath1/file212.doc",
  "path2/subpath2/file221.doc",
  "path2/subpath2/file222.doc",
  "path2/additionalpath3/additionalpath1/file2311.doc"
];

let treePath = {};
paths.forEach(path => {
  let levels = path.split("/");
  let file = levels.pop();

  levels.reduce((prev, lvl, i) => {
    return prev[lvl] = (levels.length - i - 1) ? prev[lvl] || {} : (prev[lvl] || []).concat([file]);
  }, treePath);
});

console.log(treePath);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

你可以这样做。

var urls = [
"path1/subpath1/file1.doc","path1/subpath1/file2.doc","path1/subpath2/file1.doc","path1/subpath2/file2.doc","path2/subpath1/file1.doc","path2/subpath1/file2.doc","path2/subpath2/file1.doc","path2/subpath2/file2.doc","path2/subpath2/additionalpath1/file1.doc"
]

function getMap(urls){
var map = {};

urls.forEach(function(url){
    var parts = url.split("/");
    makePath(map, parts);
})
return map;
}

function makePath(map,parts){
    var currentPath = map;
    for(var i = 0 ; i < parts.length - 1 ; i++ ){
            if(i == parts.length -2 ){
                currentPath[parts[i]]  = currentPath[parts[i]] || [];
                currentPath[parts[i]].push(parts[++i]);
            }else{
                currentPath[parts[i]] =  currentPath[parts[i]] || {};
                currentPath = currentPath[parts[i]];
            }
    }

}
getMap()

为你留下一部分:)

答案 4 :(得分:0)

也许您也希望在数组中也有子目录


const path = '/home/user/dir/file';
var next;

path.split('/').reverse().forEach(name => {
  if (name === "") return;
  if (typeof(next) === 'undefined') {
    next = [String(name)];
  } else {
    var current = {};
    current[name] = next;
    next = [current];
  }
});
console.log(JSON.stringify(next));
// [{"home":[{"user":[{"dir":["file"]}]}]}]

然后可以连接对象