将 CSV/XLS 转换为 JSON?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/662859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:12:50  来源:igfitidea点击:

converting CSV/XLS to JSON?

jsoncsvxls

提问by mkoryak

Does anyone know if there is application that will let me convert preferably XLS to JSON?

有谁知道是否有应用程序可以让我最好将 XLS 转换为 JSON?

I'll also settle for a converter from CSV since that's what I'll probably end up having to write myself if there is nothing around.

我也将接受 CSV 的转换器,因为如果周围没有任何东西,我可能最终不得不自己写。

采纳答案by zmonteca

This worked perfectly for me and does NOT require a file upload:

这对我来说非常有效,并且不需要文件上传:

https://github.com/cparker15/csv-to-json?files=1

https://github.com/cparker15/csv-to-json?files=1

回答by Shan Carter

You can try this tool I made:

你可以试试我做的这个工具:

Mr. Data Converter

数据转换器先生

It converts to JSON, XML and others.

它转换为 JSON、XML 和其他格式。

It's all client side, too, so your data never leaves your computer.

这也是所有客户端,因此您的数据永远不会离开您的计算机。

回答by knb

Since Powershell 3.0 (shipped with Windows 8, available for Windows 7 and windows Server 2008but not Windows Vista ) you can use the built-in convertto-json commandlet:

从 Powershell 3.0(随 Windows 8 一起提供,可用于 Windows 7 和 Windows Server 2008但不适用于 Windows Vista),您可以使用内置的 convertto-json commandlet:

PS E:> $topicsjson = import-csv .\itinerary-all.csv | ConvertTo-Json 

PS E:\> $topicsjson.Length
11909

PS E:\> $topicsjson.getType()

IsPublic IsSerial Name                                     BaseType                  
-------- -------- ----                                     --------                  
True     True     Object[]                                 System.Array              

Online Help Page on Technet

Technet 上的在线帮助页面

回答by Matt York

If you can't find an existing solution it's pretty easy to build a basic one in Java. I just wrote one for a client and it took only a couple hours including researching tools.

如果您找不到现有的解决方案,则可以很容易地用 Java 构建一个基本的解决方案。我刚刚为客户写了一个,包括研究工具在内只花了几个小时。

Apache POI will read the Excel binary. http://poi.apache.org/

Apache POI 将读取 Excel 二进制文件。 http://poi.apache.org/

JSONObject will build the JSON

JSONObject 将构建 JSON

After that it's just a matter of iterating through the rows in the Excel data and building a JSON structure. Here's some pseudo code for the basic usage.

之后,只需遍历 Excel 数据中的行并构建 JSON 结构即可。下面是一些基本用法的伪代码。

FileInputStream inp = new FileInputStream( file );
Workbook workbook = WorkbookFactory.create( inp );

// Get the first Sheet.
Sheet sheet = workbook.getSheetAt( 0 );

    // Start constructing JSON.
    JSONObject json = new JSONObject();

    // Iterate through the rows.
    JSONArray rows = new JSONArray();
    for ( Iterator<Row> rowsIT = sheet.rowIterator(); rowsIT.hasNext(); )
    {
        Row row = rowsIT.next();
        JSONObject jRow = new JSONObject();

        // Iterate through the cells.
        JSONArray cells = new JSONArray();
        for ( Iterator<Cell> cellsIT = row.cellIterator(); cellsIT.hasNext(); )
        {
            Cell cell = cellsIT.next();
            cells.put( cell.getStringCellValue() );
        }
        jRow.put( "cell", cells );
        rows.put( jRow );
    }

    // Create the JSON.
    json.put( "rows", rows );

// Get the JSON text.
return json.toString();

回答by dataman

This works for me and runs client-side: http://www.convertcsv.com/csv-to-json.htm

这对我有用并运行客户端:http: //www.convertcsv.com/csv-to-json.htm

回答by DanDan

I just found this:

我刚刚发现了这个:

http://tamlyn.org/tools/csv2json/

http://tamlyn.org/tools/csv2json/

( Note: you have to have your csv file available via a web address )

(注意:您必须通过网址提供您的 csv 文件)

回答by Keyang

Take a try on the tiny free tool:

试试这个小小的免费工具:

http://keyangxiang.com/csvtojson/

http://keyangxiang.com/csvtojson/

It utilises node.js csvtojson module

它利用 node.js csvtojson 模块

回答by Tronic

None of the existing solutions worked, so I quickly hacked together a script that would do the job. Also converts empty strings into nulls and and separates the header row for JSON. May need to be tuned depending on the CSV dialect and charset you have.

现有的解决方案都不起作用,所以我迅速编写了一个脚本来完成这项工作。还将空字符串转换为空字符串并分隔 JSON 的标题行。可能需要根据您拥有的 CSV 方言和字符集进行调整。

#!/usr/bin/python
import csv, json
csvreader = csv.reader(open('data.csv', 'rb'), delimiter='\t', quotechar='"')
data = []
for row in csvreader:
    r = []
    for field in row:
        if field == '': field = None
        else: field = unicode(field, 'ISO-8859-1')
        r.append(field)
    data.append(r)
jsonStruct = {
    'header': data[0],
    'data': data[1:]
}
open('data.json', 'wb').write(json.dumps(jsonStruct))

回答by StaxMan

Instead of hard-coded converters, how about CSV support for Hymanson (JSON processor): https://github.com/FasterXML/Hymanson-dataformat-csv. So core Hymanson can read JSON in as POJOs, Maps, JsonNode, almost anything. And CSV support can do the same with CSV. Combine the two and it's very powerful but simple converter between multiple formats (there are backends for XML, YAML already, and more being added).

代替硬编码转换器,Hyman逊(JSON 处理器)的 CSV 支持如何:https://github.com/FasterXML/Hymanson-dataformat-csv。因此,核心 Hymanson 可以将 JSON 读取为 POJO、MapsJsonNode等几乎任何东西。CSV 支持可以对 CSV 执行相同的操作。将两者结合起来,它是多种格式之间非常强大但简单的转换器(已经有 XML、YAML 的后端,并且正在添加更多)。

An article that shows how to do this can be found here.

可以在此处找到说明如何执行此操作的文章。

回答by qxotk

See if this helps: Back to CSV - Convert CSV text to Objects; via JSON

看看这是否有帮助:返回 CSV - 将 CSV 文本转换为对象;通过 JSON

This is a blog post published in November 2008 that includes C# code to provide a solution.

这是 2008 年 11 月发布的博客文章,其中包含用于提供解决方案的 C# 代码。

From the intro on the blog post:

来自博客文章的介绍:

As Json is easier to read and write then Xml. It follows that CSV (comma seperated values) is easier to read and write then Json. CSV also has tools such as Excel and others that make it easy to work with and create. So if you ever want to create a config or data file for your next app, here is some code to convert CSV to JSON to POCO objects

由于 Json 比 Xml 更容易读写。因此,CSV(逗号分隔值)比 Json 更易于读写。CSV 还具有 Excel 等工具,可以轻松使用和创建。因此,如果您想为下一个应用程序创建配置或数据文件,这里有一些代码可以将 CSV 转换为 JSON 到 POCO 对象