简体   繁体   English

如何将CSV数据转换为JSON对象

[英]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: 可能的工作流程:

  • php upload page for CSV file / or upload by FTP/SSH CSV文件的php上传页面/或通过FTP / SSH上传
  • csv is on server CSV在服务器上
  • php page with param datafromid=csvfile provides the data conversion from csv to json.. (if the csv file does not contain too many rows, read it into an php array, and spit it out with json_encode() by getting each row ( fgetcsv() ) and adding a sub-array to your total array) 具有参数datafromid = csvfile的php页面提供了从csv到json的数据转换。(如果csv文件不包含太多行,则将其读取到php数组中,并通过获取每一行( fgetcsv 将其吐出json_encode() () )并将一个子数组添加到您的总数组中)
  • depending on how you provide the data to d3, use ajax to access that php-page to access the json or include it in your page (i think loading it by ajax, accessing the file from the browser where the page lies, is better - you may use jQuery get() ) 根据您向d3提供数据的方式,使用ajax访问该php-page以访问json或将其包含在页面中(我认为通过ajax加载它,从页面所在的浏览器访问文件会更好-您可以使用jQuery get()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM