如何CSV数据转换成JSON对象 [英] How to convert CSV Data to JSON Object

查看:354
本文介绍了如何CSV数据转换成JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个csv文件(CSV文件可能会坐在服务器端preferably,但尚未决定)。最有可能的用户将是presented从这些CSV文件,选择从下拉菜单中的选项。然后selcting一个特定的文件后,用户点击输入,那么这个选择的CSV文件应在JavaScript作为数组读取。因此,如何做到这一点? -

So I have this csv file (the csv file might sit on server side preferably , but not decided yet). Most probably the user will be presented the options of selecting from these csv files from a drop-down menu. Then after selcting a particular file, user clicks "enter" , then this selected csv file should be read in javascript as an array. So how to do this ?? :-

我的问题是关于<一个行href=\"http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript?lq=1\">How传递变量和数据从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,

为了读它手动这是做了什么。但你可以看到它有所有的硬csv_data $ C $的CD,但我希望得到它的自动(即从一个CSV文件中读取获取生成)

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 (i.e. get generated by reading from a csv file )

    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"}
     ]
    }
   ]
  }




 ]
};

现在的问题是,我想的JavaScript只是读取本地位置这个csv文件。但是怎么样了它可以读取一个CSV文件导入在JavaScript这样一个复杂的维数组?

The problem now is that I want the javascript to just read this csv file from local location. But hows it possible to read a CSV file into such a complex dimensional array in JAVASCRIPT ?

推荐答案

可能的设置:

PHP的Web服务器(或服务器的NodeJS - 我个人最喜欢的)

PHP web server (or NodeJS server - my personal favourite)

可能的工作流程:


    为CSV文件
  • PHP上传网页/或通过FTP / SSH上传

  • CSV是在服务器

  • 与参数datafromid = csvfile PHP页面提供了从CSV到JSON数据转换。(如果CSV文件不包含太多行,它读入一个PHP数组,并用的的json_en code> json_en code()由让每一行( fgetcsv())并添加子阵列的总数组)

  • 取决于你如何提供数据D3,使用Ajax来访问PHP页面访问JSON或包括在您的网页(我认为阿贾克斯加载它,从浏览器所在的页面所在访问文件,更好的是 - 你可以使用 jQuery的获得()

  • php upload page for CSV file / or upload by FTP/SSH
  • csv is on server
  • 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)
  • 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())

这篇关于如何CSV数据转换成JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆