如何最好地将字符串路径数组转换为 JSON?

问题描述

我需要将一个字符串路径数组转换为没有重复的嵌套 JSON。

我正在尝试从字符串路径数组构建一个 JSON 文件。在构建 JSON 时,我希望嵌套重复的文件名称(即“目录”将“产品”和“非产品”作为子项)。每个对象都有一个 Specification<?> spec = spec1.or(spec2); spec = spec.and(spec3); 一个可选的 name 数组。

children

let paths = [ "catalog/product/category/sub-category/page","catalog/non-product/page" ]; let pages = {}; paths.forEach(path => { let levels = path.split("/"); let file = levels.pop(); // iterate over each path and build JSON }); console.log(pages); 的理想输出是:

pages

解决方法

这是构建目录树的示例:

const paths = [
   "catalog/product/category/sub-category/page","catalog/non-product/page","test/2"
];

const directoryTree = {
   name: '/',children: []
};

paths.forEach(path => {
   const directorySegments = path.split("/");

   let currentDirectory = directoryTree;

   directorySegments.forEach(segment => {
      const child = currentDirectory.children.find(path => path.name.toLowerCase() === segment.toLowerCase());

      if (child !== undefined) {
         currentDirectory = child;
      }
      else {
         const newDirectory = {
            name: segment,children: []
         };

         currentDirectory.children.push(newDirectory);

         currentDirectory = newDirectory;
      }
   });
});

const directoryJSON = JSON.stringify(directoryTree);

如果您需要删除空目录的 children 属性,您可以这样修改代码:

const paths = [
   "catalog/product/category/sub-category/page","test/2"
];

const directoryTree = {
   name: '/'
};

paths.forEach(path => {
   const directorySegments = path.split("/");

   let currentDirectory = directoryTree;

   directorySegments.forEach(segment => {
      let child;

      if (currentDirectory.children !== undefined) {
         child = currentDirectory.children.find(path => path.name.toLowerCase() === segment.toLowerCase());
      }
      else {
         currentDirectory.children = [];
      }

      if (child !== undefined) {
         currentDirectory = child;
      }
      else {
         const newDirectory = {
            name: segment
         };

         currentDirectory.children.push(newDirectory);

         currentDirectory = newDirectory;
      }
   });
});

const directoryJSON = JSON.stringify(directoryTree);

它将产生以下 JSON 结果:

{
   "name":"/","children":[
      {
         "name":"catalog","children":[
            {
               "name":"product","children":[
                  {
                     "name":"category","children":[
                        {
                           "name":"sub-category","children":[
                              {
                                 "name":"page"
                              }
                           ]
                        }
                     ]
                  }
               ]
            },{
               "name":"non-product","children":[
                  {
                     "name":"page"
                  }
               ]
            }
         ]
      },{
         "name":"test","children":[
            {
               "name":"2"
            }
         ]
      }
   ]
}

如您所见,我使用根目录(“/”)来保存树。如果“目录”是您的根目录,您可以排除它。