本文介绍了如何使用jquery将csv文件转换为json并下载该json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的csv文件像这样 $(function(){var csv = $(#fileUpload)。val(); $(#upload)。bind(click,function(){var regex = / ^ Z0-9\s_\\.\ - :])+(。csv | .txt)$ /; if(regex.test($(#fileUpload)。val()。toLowerCase ){if(typeof(FileReader)!=undefined){var reader = new FileReader(); reader.onload = function(e){var table = $(< table /> e.target.result.split(\\\); for(var i = 0; i < script src =https: /ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js\"> ;</script> < input type =fileid =fileUploadname =fileUpload/> < input type =buttonid =uploadvalue =上传name =上传/> < hr /> < div id =dvCSV>< / div> 在上面的代码中,我需要转换上传的csv文件为json格式使用jquery.how做到这一点?和创建的json格式应该下载为json file.please帮助我如何做这个 > 解决方案这是您想要的功能: $(function(){ var csv = $(#fileUpload)。val(); $ ,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\\\); if(rows.length> 0){ var firstRowCells = GetCSVCells(rows [0],,); var dataArray = new Array(); (var i = 1; 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(''); $(#dvCSV)。append(JSON.stringify(dataArray)); } } reader.readAsText($(#fileUpload)[0] .files [0]); } else { alert(此浏览器不支持HTML5。 } } else { alert(请上传有效的CSV文件。 } }); }); 函数GetCSVCells(row,separator){ return row.split(separator); } 它将CSV内容转换为从中读取属性的对象数组第一行(标题行),然后将其转换为JSON字符串。 my csv file is like this $(function () { 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 table = $("<table />"); var rows = e.target.result.split("\n"); for (var i = 0; i < rows.length; i++) { var row = $("<tr />"); var cells = rows[i].split(","); for (var j = 0; j < cells.length; j++) { var cell = $("<td />"); cell.html(cells[j]); row.append(cell); } table.append(row); } $("#dvCSV").html(''); $("#dvCSV").append(table); } reader.readAsText($("#fileUpload")[0].files[0]); } else { alert("This browser does not support HTML5."); } } else { alert("Please upload a valid CSV file."); } }); });<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>In the above code i need to convert uploaded csv file to json format using jquery.how to do this? and created json format should be downloaded as json file.please help me how to do this 解决方案 Here is the function you want:$(function () { 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 firstRowCells = GetCSVCells(rows[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(''); $("#dvCSV").append(JSON.stringify(dataArray)); } } 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);}It converts the CSV content to an array of objects in which properties are read from the first line (header line) and then it is "converted" to a JSON string. 这篇关于如何使用jquery将csv文件转换为json并下载该json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 15:40