json转换算法

把一个只有一个层级的json文件导入到多个层级的json文件中,如下把

{
error_system_fail:'111',
home_tip:'222',
setting_name_save:'233',
setting_station_name_save:'333',
list_item_title_name:'355'
}

转换为

{
   error:{
     system_fail:'111'
   },
   home:{
     tip:'222'
   },
   setting: {
     name:{
      save:'233'
     },
     station_name:{
      save:'333'
     }
   },
   list:{
     item_title_name:'355'
   }
}

只能通过算法实现

"use strict";

const readLine = require('readline');
let rl = readLine.createInterface({
    input: process.stdin,
    output: process.stdout,
    // 其他参数省略
})
let templateName=''
rl.question('请输入你要转换的语言名称 ', function(name) {
    //templateName = name+'.json';
    switch (name.trim()) {
        case "英语":
            templateName='en_US.json';
            break;
        case "西班牙语":
            templateName='es_ES.json';
            break;
        default:
            console.log("没有找到该语言模板!");
            break;
    }
    console.log('这是你输入的模板文件名:' + templateName);
    rl.close();
    if(templateName){
        const fs = require("fs");
        // 待插入单层json文件
        let map = JSON.parse(fs.readFileSync("struct.json"));
        
        // 模板json文件(多层json)
        let struct = JSON.parse(fs.readFileSync(templateName));
        
        function isLeafNode(node) {
            return typeof node == 'string';
        }
        
        function isEmptyNode(node) {
            return node == undefined;
        }
        
        //用_拆分map里的key
        function gen(map, struct) {
            for (var key in map) {
                let subkeys = key.split('_');
                var paths = [];
                var curPath = [];
                genPaths(paths, curPath, subkeys, 0);
                //console.log(paths);
                paths.forEach(e => {
                    write(e, struct, map[key]);
                });
            }
        }
        // 穷举所有组合
        function genPaths(gen, curPath, path, depth) {
            if (depth >= path.length) {
                gen.push(curPath);
                return;
            }
            let newPath = [...curPath, path[depth]];
            genPaths(gen, newPath, path, depth + 1);
            if (curPath.length > 0) {
                let newPath1 = curPath;
                newPath1[curPath.length - 1] += ('_' + path[depth]);
                genPaths(gen, newPath1, path, depth + 1);
            }
        }
        
        // 尝试匹配叶子节点并写入数据
        function write(path, struct, value) {
            var prev = {};
            var subpath;
            for (var i = 0; i < path.length; i++) {
                subpath = path[i];
                if (isEmptyNode(struct[subpath])) {
                    return;
                }
                prev = struct;
                struct = struct[subpath];
            }
            if (subpath && isLeafNode(prev[subpath])) {
                prev[subpath] = value;
            }
        }
        
        gen(map, struct);
        
        //let data = JSON.stringify(struct); // 单行json
        //console.log(struct)
        let data = JSON.stringify(struct, null, 2); // 格式化json
        fs.writeFileSync(templateName, data, 'utf8');
        
        console.log("This is after the write call");
        console.log("生成新"+templateName)
        }
})

 

posted @ 2023-07-10 16:57  方小川  阅读(22)  评论(0编辑  收藏  举报