csv-转化为json格式批量处理的

var fs = require('fs');
var iconv = require('iconv-lite');
var csvParse = require('csv-parse/lib/sync');
// var jsonh = require('./jsonh');
// const zlib = require('zlib');

let clientCfg = null;


function AttributeCount(obj) {
    var count = 0;
    for(var i in obj) {
        if(obj.hasOwnProperty(i)) {  // 建议加上判断,如果没有扩展对象属性可以不加
            count++;
        }
    }
    return count;
}

function Uint8ArrayToString(fileData){
    var dataString = "";
    for (var i = 0; i < fileData.length; i++) {
      dataString += String.fromCharCode(fileData[i]);
    }
   
    return dataString;
}

function stringToUint8Array(str){
    var arr = [];
    for (var i = 0, j = str.length; i < j; ++i) {
        arr.push(str.charCodeAt(i));
    }

    var tmpUint8Array = new Uint8Array(arr);
    return tmpUint8Array;
}

function setOneRow(obj, field, data)
{
    let children = field.split(".");  

    if (children.length < 2)
    {
        obj[field] = parseData(data);
        return;
    }

    var curNode = obj;

    for (var i = 0; i < children.length - 1; i++)
    {
        let isArray = !isNaN(children[i + 1]);

        if (curNode[children[i]] == undefined)
        {
            if (isArray)
            {
                curNode[children[i]] = [];
            }
            else
            {
                curNode[children[i]] = {};
            }
        }

        curNode = curNode[children[i]];
    }

    curNode[children[i]] = parseData(data);
    //console.log("setobj");
}

//解析单元格
function parseData(data)
{
    if (data == "")//空字符串视为字符串类型
    {
        return data;
    }
    else if (isNaN(data))
    {
        if (data.length >= 2 && data[0] == "\"" && data[data.length - 1] == "\"")
        {
            return data.substr(1, data.length - 2);
        }
        return data;
    }
    else if (data.indexOf(".") > 0)
    {
        return parseFloat(data);
    }
    else
    {
        return parseInt(data);
    }
}

function checkJsonString(obj)
{
    if (obj.hasOwnProperty(""))
    {
        delete obj[""];
    }

    let replaceRegex = /\\n/g;

    //检查JSON对象的每个字段是否是JSON字串
    for (var i in obj) 
    {        
        if (obj[i].constructor == String)
        {
            obj[i] = obj[i].replace(replaceRegex, '\n');
        }

        try{
            //console.log("[checkJsonString] key:" + i + " value:" + obj[i]);
            let parseObj = JSON.parse(obj[i]);

            switch (parseObj.constructor)
            {
                case Number:
                    obj[i] = parseObj;
                    break;
                case Object:
                    obj[i] = parseObj;
                    break;
                case Array:
                    obj[i] = parseObj;
                    break;
            }
        }
        catch (ex)
        {
            //console.log("[checkJsonString] key:" + i + " value:" + obj[i]);
        }        
    }
}

function convertOneFile(srcPath, desPath, tableName)
{
    let data = fs.readFileSync(srcPath);

    let content = iconv.decode(data, "gbk");

    let regex = /\n,+(\n|\r\n)/g;
    let emptyLineIndex = content.search(regex);

    if (emptyLineIndex > 0)
    {
        content = content.substr(0, emptyLineIndex);
    }

    let records = csvParse(content, {relax_column_count: true, /*skip_empty_lines:true, skip_lines_with_empty_values:true,*/ columns: function(header){
        //console.log(header);
        let index = header.indexOf("");

        if (index > 0)
        {
            header.splice(index);
        }

        return header;
    }});

    //console.log("records", records);
    let jsonObject = new Array();

    for (let i = 0; i < records.length; i++)
    {
        checkJsonString(records[i]);
        jsonObject.push(records[i]);
    }

    let jsonStr = jsonMin(jsonObject, desPath);

    if (checkClientUse(tableName))
    {
        clientCfg[tableName] = jsonObject;
    }

    fs.writeFileSync(desPath, jsonStr, function(err){
        if (err != null)
        {
            console.error("error:", err);
            //console.error("parse error:" + srcPath);
        }            
    });
}

// function jsonCompress(jsonStr){
//     //let plain = stringToUint8Array(jsonStr);

//     // plain = Array.<number> or Uint8Array
//     var gzip = new zlib.gzipSync(jsonStr);
//     //var compressed = gzip.compress();

//     return Uint8ArrayToString(gzip);
// }



function jsonMin(jsonObject, desPath)
{
    let jsonStr;
    
    //jsonStr = JSON.stringify(jsonObject);

    let original = jsonObject;
    let soriginal = JSON.stringify(original);
    //let packed = jsonh.pack(original);
    //let spacked = JSON.stringify(packed);
    
    jsonStr = soriginal;

    return jsonStr;
}



function excel2json(srcDir, files, curpath){    

    let desDir =  curpath
    console.log("excel2json;", desDir)
    let count = 0;

    clientCfg = {};

    files.forEach(filename => {
        let dotIndex = filename.indexOf(".csv");
        let subIndex = filename.indexOf("-");//中文文件名用减号分隔,转换时只使用减号前的文件名DropConfig-掉落 => DropConfig

        let splitIndex = dotIndex;

        if (subIndex > 0)
        {
            splitIndex = subIndex;
        }

        if (splitIndex > 0)
        {
            let tableName = filename.substr(0, splitIndex);
            let desPath = desDir + tableName + ".json";

            //console.log("desPath:" + desPath);
            convertOneFile(srcDir + filename, desPath, tableName);

            count++;
        }        
    });

    console.log("covnet complete, count:" + count);

    let clientTableCount = AttributeCount(clientCfg);
    if (clientTableCount > 0)
    {
        let clentCfgStr = JSON.stringify(clientCfg);
        
        if (clentCfgStr.length > 0)
        {
            fs.writeFileSync(desDir + "ClientConfig.json", clentCfgStr, function(err){
                if (err != null)
                {
                    console.error("error:", err);
                }            
            });
        }
    }

    console.log("clientTableCount:" + clientTableCount);

    clientCfg = null;
}


function testFiles()
{
    let srcDir = "tables\\csv\\";
    let files = ["HelpConfig-帮助.csv"];
 
    excel2json(srcDir, files);
}

//按字母排序
let clientList = ['BackDropConfig',
        'ItemConfig',
        'FirstRechargeConfig',
        'PkgLevelConfig',
        'CharacterAvatarConfig'
];

function checkClientUse(name)
{
    //return true;
    if (clientList)
    {
        return clientList.includes(name);
    }

    return false;
}

function main( ){ 
    fs.readFile('config.json',function(err,data){//读取同目录下的book.json文件
        if(err){throw err;}
        
        var jsonObj = JSON.parse(data);//获取json文件对象
        // console.log("jsonObj:", jsonObj)
       
        let srcDir =  jsonObj.srcDir 
        // console.log("srcDir:", srcDir)
        let files = fs.readdirSync(srcDir);
    
        console.log("files:", files)
    
        excel2json(srcDir, files, jsonObj.desDir);
    });

   
}

main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间溜走了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值