【问题标题】:Node.js convert directory tree to JSONNode.js 将目录树转换为 JSON
【发布时间】:2021-12-26 16:05:15
【问题描述】:

预期是什么?:
我想转换下面列出的目录结构 到单个 JSON 文件中。 目录结构包含 JSON 文件 应该包含在 输出文件也是如此。

限制:
Node.js

问题:
什么是好的/有效的 生成所需输出的方法, 使用 Node.js + 模块? 从逻辑上讲,会是什么 必要的步骤?

目录结构:

CMS/
├── en/
|   ├──brand_one/
|   |  ├──footer.json
|   |  ├──header.json
|   ├──brand_two/
|   |  ├──footer.json
|   |  ├──header.json
|   ├──brand_three/
|   |  ├──footer.json
|   |  ├──header.json
├── de/
|   ├──brand_one/
|   |  ├──footer.json
|   |  ├──header.json
|   ├──brand_two/
|   |  ├──footer.json
|   |  ├──header.json
|   ├──brand_three/
|   |  ├──footer.json
|   |  ├──header.json
├── fr/
|   ├──brand_one/
|   |  ├──footer.json
|   |  ├──header.json
|   ├──brand_two/
|   |  ├──footer.json
|   |  ├──header.json
|   ├──brand_three/
|   |  ├──footer.json
|   |  ├──header.json
├── es/
|   ├──brand_one/
|   |  ├──footer.json
|   |  ├──header.json
|   ├──brand_two/
|   |  ├──footer.json
|   |  ├──header.json
|   ├──brand_three/
|   |  ├──footer.json
|   |  ├──header.json

[...]

期望的输出:

// content.json

{
  "en":[
    {
      "brand_one":{
        "footer":{
          "val": "value",
          [...]
        },
        "header":{
          "val": "value",
          [...]
        }
      },
      "brand_two":{
        "footer":{
          "val": "value",
          [...]
        },
        "header":{
          "val": "value",
          [...]
        }
      },
      "brand_three":{
        "footer":{
          "val": "value",
          [...]
        },
        "header":{
          "val": "value",
          [...]
        }
      }
    }
  ],
  [...]
}

【问题讨论】:

标签: javascript node.js json


【解决方案1】:

你可以使用 npm 包树解析器。

https://www.npmjs.com/package/tree-parser

【讨论】:

    【解决方案2】:

    您可以创建一个函数来将目录转换为对象,每个目录/文件都有一个属性。

    这将被递归调用以遍历整个树,在本例中使用 fs/promises 函数。

    const fs = require('fs/promises'); 
    const path = require('path');
    
    async function walkDir(dir, result = {}) {
        let list = await fs.readdir(dir);
        for(let item of list) {
            const itemPath = path.join(dir, item);
            let stats = await fs.stat(itemPath)
            if (await stats.isDirectory()) {
                result[item] = {};
                await walkDir(itemPath, result[item]);
            } else {
                const fileName = path.basename(item, path.extname(item));
                result[fileName] = JSON.parse(await fs.readFile(itemPath, { encoding: 'utf-8'}));
            }
        }
        return result;
    }
    
    async function testWalkDir() {
        let result = await walkDir('./CMS')
        console.log("Result:", JSON.stringify(result, null, 2));
    }
    
    testWalkDir();
    

    假设每个文件看起来像

    {
      "some_key": "some_val"
    }
    

    我得到的结果如下所示:

    {
      "en": {
        "brand_one": {
          "footer": {
            "some_key": "some_val"
          },
          "header": {
            "some_key": "some_val"
          }
        },
        "brand_three": {
          "footer": {
            "some_key": "some_val"
          },
          "header": {
            "some_key": "some_val"
          }
        },
        "brand_two": {
          "footer": {
            "some_key": "some_val"
          },
          "header": {
            "some_key": "some_val"
          }
        }
      }
    }
    
    
    

    【讨论】:

    • 谢谢,这就是我要找的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多