[英]How to convert CSV Data to JSON Object
So I have this csv file (the csv file might sit on server side preferably , but not decided yet). 所以我有这个csv文件(csv文件最好放在服务器端,但尚未决定)。 Most probably the user will be presented the options of selecting from these csv files from a drop-down menu. 很可能会向用户显示从下拉菜单中的这些csv文件中进行选择的选项。 Then after selcting a particular file, user clicks "enter" , then this selected csv file should be read in javascript as an array. 然后,在选择特定文件后,用户单击“ enter”,然后应在javascript中将所选的csv文件作为数组读取。 So how to do this ?? 那么如何做到这一点? :- : -
My question is on the lines of How to pass variables and data from PHP to JavaScript? 我的问题是关于如何将变量和数据从PHP传递到JavaScript?
___csv_file____ ___csv_file____
Class, Subclass, Company, Product,
Chocolate, Wafer chocolate, Nestle, KitKat,
Chocolate, White chocolate, Nestle, Milkybar,
Chocolate, White chocolate, Nestle, Milkybar,
Chocolate, Caramel chocolate, Nestle, BarOne,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Cadbury, Dairy milk,
In order to read it manually this is what has been done. 为了手动阅读它,已完成了此操作。 But as you can see it has all the csv_data hardcoded, but I'd like to get it automated (ie get generated by reading from a csv file ) 但是如您所见,它具有所有csv_data硬编码,但我想使其自动化(即通过读取csv文件生成)
root = {
"name": "Chocolate", "tag":"class",
"children": [
{
"name": "Wafer", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "KitKat", "tag":"product"}
]
}
]
},
{
"name": "White", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "Milkybar", "tag":"product"}
]
}
]
},
{
"name": "Caramel", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "BarOne", "tag":"product"}
]
}
]
},
{
"name": "Milk", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "Nestle Milk", "tag":"product"}
]
}, {
"name": "Cadbury", "tag":"company",
"children": [
{"name": "Dairy Milk", "tag":"product"}
]
}
]
}
]
};
The problem now is that I want the javascript to just read this csv file from local location. 现在的问题是,我希望javascript仅从本地位置读取此csv文件。 But hows it possible to read a CSV file into such a complex dimensional array in JAVASCRIPT ? 但是,如何在JAVASCRIPT中将CSV文件读取到如此复杂的维数组中呢?
Possible setup: 可能的设置:
PHP web server (or NodeJS server - my personal favourite) PHP Web服务器(或NodeJS服务器-我个人的最爱)
Possible workflow: 可能的工作流程:
First off on the server create a PHP file giving it a relevant name like candyData.php in which you'd have: 首先在服务器上创建一个PHP文件,为它提供一个相关的名称,例如candyData.php,您将在其中具有以下名称:
//code to read your data.csv
//code which stores the data from the csv into an object $candyData
$json = json_encode($candyData);
return $json
Then on the client side with Javascript and I'm going to also use JQuery: 然后在客户端使用Javascript,我还将使用JQuery:
jQuery.ajax({
url: "http://example.mysite.com/rest/candyData.php",
type: "GET",
success: function (response) {
var candyData = response;
//Here you'd put all the code you currently have so you can visualize the data
},
error: function (response) {
console.log("failed");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.