【问题标题】:How best to convert an array of string paths to JSON?如何最好地将字符串路径数组转换为 JSON?
【发布时间】:2021-05-09 15:01:39
【问题描述】:

我需要将一个字符串路径数组转换为嵌套的 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"
                }
            ]
        }
    ]
}

【问题讨论】:

  • 到目前为止,您自己尝试过什么来解决这个问题?
  • "// 遍历每个路径并构建 JSON" - 如果不是字符串,则不是 JSON。预期的结果是一个对象。
  • @Andreas 我在reduce 上花了几个小时,但无法正确嵌套
  • 我试图调整 this solution 但我无法让它适用于具有两个以上嵌套文件夹的路径

标签: javascript json pathname


【解决方案1】:

这是一个构建目录树的示例:

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"
            }
         ]
      }
   ]
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 2017-07-20
    • 2015-04-05
    • 2011-11-22
    • 2022-01-27
    相关资源
    最近更新 更多