Convert CSV data into JSON format using Javascript

后端 未结 6 2398
谎友^
谎友^ 2020-11-27 04:24

I have data in CSV format data and want to convert into JSON format using Javascript.

Following are csv format:

[Test.csv] 
id;name;author
integer;st         


        
6条回答
  •  自闭症患者
    2020-11-27 04:54

    The below should work for you.

    All credit to http://techslides.com/convert-csv-to-json-in-javascript

    //var csv is the CSV file with headers
    function csvJSON(csv){
    
      var lines=csv.split("\n");
    
      var result = [];
    
      // NOTE: If your columns contain commas in their values, you'll need
      // to deal with those before doing the next step 
      // (you might convert them to &&& or something, then covert them back later)
      // jsfiddle showing the issue https://jsfiddle.net/
      var headers=lines[0].split(",");
    
      for(var i=1;i

提交回复
热议问题