如何构建一个csv以获得可读的json文件?

时间:2018-03-31 12:37:05

标签: javascript jquery json csv

我对如何构建csv文件感到有些困惑,目前我这样做:

enter image description here

但是,当我解析它时,给我一个json,如:

[
  {
    "20": "15",
    "New York": "Singapore",
    "Employee": "Employee"
  }
]

我应该如何构造csv以正确输出相关值'

这是一个jsFiddle来玩

2 个答案:

答案 0 :(得分:0)

CSV文件基本上只是多行上逗号分隔值的列表。

您所要做的就是浏览CSV字符串并从中填充Javascript对象。

这个快速功能背后的基本逻辑是newline每个,上的字符串,然后在每个const csv = `New York,Employee,20 Singapore,Employee,15`; const csvToObject = s => s.split('\n').map(line => line.split(',')); console.log(csvToObject(csv));上再次拆分:



VideoWidget::start()
{

    qDebug()<<QOpenGLWidget::defaultFramebufferObject() ;


    auto playlist = new QMediaPlaylist(this);
    //   playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
    playlist->addMedia(QUrl::fromLocalFile("C:/Users/COMPUTER/Desktop/streamvideo 340.mp4"));
    QThread *thread1 = QThread::currentThread();

    qDebug() << "play:"<<thread1->currentThreadId();
    frameProcessor = new OboVideoSurface(this);
    connect(frameProcessor, SIGNAL(newVideoFrame(QVideoFrame)), this, SLOT(updateVideoFrame(QVideoFrame)));
    QMediaPlayer*   mediaPlayer = new QMediaPlayer(this);

    mediaPlayer->setVideoOutput(frameProcessor);
    mediaPlayer->setPlaylist(playlist);
    mediaPlayer->setMuted(true);

    mediaPlayer->play();


}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

CT,TY,VA
NY,EMP,20
AL,EMP,10

上面的csv格式产生了正确格式化的JSON。第一行为每列指定了JSON中的标题或键。

所以在第一行给出一个适合你的名字,然后生成的JSON将链接到第一行。

或硬编码数组中的键,如var firstRowCells = ['firstKey','secondKey','thirdKey'];

&#13;
&#13;
$(function () {
    var firstRowCells = ['firstKey','secondKey','thirdKey'];
    var csv = $("#fileUpload").val();

    $("#upload").bind("click", function () {
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;
        if (regex.test($("#fileUpload").val().toLowerCase())) {
            if (typeof (FileReader) != "undefined") {
                var reader = new FileReader();
                reader.onload = function (e) {
                    var rows = e.target.result.split("\r\n");

                    if(rows.length>0){

                        var dataArray = new Array();
                        for(var i=1;i<rows.length;i++)
                        {
                            var cells = GetCSVCells(rows[i], ",");
                            var obj = {};
                            for(var j=0;j<cells.length;j++)
                            {
                                obj[firstRowCells[j]] = cells[j];
                            }
                            dataArray.push(obj);
                        }

                        $("#dvCSV").html('');
                        var arr = (dataArray);
                        $("#dvCSV").append(JSON.stringify(arr, null, '  '));
                    }
                }
                reader.readAsText($("#fileUpload")[0].files[0]);
            } else {
                alert("This browser does not support HTML5.");
            }
        } else {
            alert("Please upload a valid CSV file.");
        }
    });
});

function GetCSVCells(row, separator){
    return row.split(separator);
}
&#13;
#dvCSV {
  white-space: pre-wrap
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <input type="file" id="fileUpload" name="fileUpload" />
  <input type="button" id="upload" value="Upload" name="upload"/>
  <hr />
<div id="dvCSV"></div>
&#13;
&#13;
&#13;