如何使用javascript将csv数组对象转换为json

时间:2017-10-13 07:34:34

标签: javascript node.js

data = [ 'Txn Date,"Value\rDate",Description,"Ref No./Cheque\rNo.",Debit,Credit,Balance',
  '1 Jan 2015,1 Jan 2015,"BY TRANSFER-\rNEFT*SCBL0036001*NB20501\r501011778*MR SELVAKUMAR\rAR-","TRANSFER\rFROM\r3199677044304",,"65,000.00","7,77,065.65"'
  ]

想要将上述数据格式转换为json格式。为此,我使用的是分割函数,即split(",");,但我的double值包含“,”。

function csvJSON( tabledata){

    var lines = tabledata;//csv.split("\n");

    var result = [];

    var headers=lines[0].split(",");

    for(var i=1;i<lines.length;i++){

        var obj = {};



        var currentline = lines[i].split(",");


        for(var j=0;j<headers.length;j++){


                obj[headers[j]] = currentline[j];



        }

        result.push(obj);

    }

    return JSON.stringify(result); //JSON
}

这就是我得到的,

{ 'Txn Date': '1 Jan 2015',
        '"Value\rDate"': '1 Jan 2015',
        Description: '"BY TRANSFER-\rNEFT*SCBL0036001*NB20501\r501011778*MR SELVAKUMAR\rAR-"',
        '"Ref No./Cheque\rNo."': '"TRANSFER\rFROM\r3199677044304"',
        Debit: '',
        Credit: '"65',
        Balance: '000.00"' }

预期产量:

{ 'Txn Date': '1 Jan 2015',
        '"Value\rDate"': '1 Jan 2015',
        Description: '"BY TRANSFER-\rNEFT*SCBL0036001*NB20501\r501011778*MR SELVAKUMAR\rAR-"',
        '"Ref No./Cheque\rNo."': '"TRANSFER\rFROM\r3199677044304"',
        Debit: '',
        Credit: '65,000.00',
        Balance: '7,77,065.65' }

如何解决这个问题,任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用此功能解析csv CSVToArray()

public void createPdf(String dest) throws IOException, FileNotFoundException{
    PdfWriter writer = new PdfWriter(dest);
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document doc = new Document(pdfDoc);
    pdfDoc.setDefaultPageSize(PageSize.A5);//All pages will be added using this page size
    String paragraphOneText = "I have seen the face of sorrow\n" +
            "She looks away in the distance\n" +
            "Across all these bridges\n" +
            "From whence I came\n" +
            "And those spans, trussed and arched\n" +
            "Hold up our lives as we go back again\n" +
            "To how we thought then\n" +
            "To how we thought we thought then";
    String paragraphTwoText = "I have seen sorrow's face,\n" +
            "But she is ever turned away\n" +
            "And her words leave me blind\n" +
            "Her eyes make me mute\n" +
            "I do not understand what she says to me\n" +
            "I do not know if to obey\n" +
            "Or attempt a flood of tears";
    String paragraphThreeText  = "I have seen her face\n" +
            "She does not speak\n" +
            "She does not weep\n" +
            "She does not know me\n" +
            "For I am but a stone fitted in place\n" +
            "On the bridge where she walks";
    String attribution = "--Toc the Younger";

    Paragraph p = new Paragraph(paragraphOneText);
    //Current default pagesize is A5, so any new pages will be created as A5
    doc.add(p);
    //Changing default pagesize will affect any new pages that are created
    pdfDoc.setDefaultPageSize(PageSize.A5.rotate());
    //Adding an areabreak of type NEXT_PAGE will force the creation of a new page
    doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    p = new Paragraph(paragraphTwoText);
    doc.add(p);
    pdfDoc.setDefaultPageSize(PageSize.A5);
    doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    p = new Paragraph(paragraphThreeText);
    doc.add(p);
    p= new Paragraph(attribution);
    doc.add(p);
    doc.close();
}

之后你只需要保存json