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

How best to convert an array of string paths to JSON?

我需要将字符串路径数组转换为嵌套的 JSON,不重复。

我正在尝试从字符串路径数组构建一个 JSON 文件。在构建 JSON 时,我希望嵌套重复的文件夹名称(即“目录”将具有“产品”和“非产品”作为子项)。每个对象都有一个 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 的理想输出是:

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

下面是构建目录树的示例:

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",
   "catalog/non-product/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"
            }
         ]
      }
   ]
}

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