有谁知道是否有应用程序可以让我最好将 XLS 转换为 JSON?
我也会满足于 CSV 的转换器,因为如果周围没有任何东西,我可能最终不得不自己写。
这对我来说非常有效,不需要文件上传:
由于 Powershell 3.0(随 Windows 8 提供,可用于 Windows 7 和 Windows Server 2008但不适用于 Windows Vista),您可以使用内置的 convertto-json 命令行开关:
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
如果您找不到现有的解决方案,那么用 Java 构建一个基本的解决方案非常容易。我刚刚为客户写了一个,只花了几个小时,包括研究工具。
Apache POI 将读取 Excel 二进制文件。 http://poi.apache.org/
JSONObject 将构建 JSON
之后,只需遍历 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();
这对我有用并在客户端运行:http: //www.convertcsv.com/csv-to-json.htm
现有的解决方案都不起作用,所以我很快就编写了一个可以完成这项工作的脚本。还将空字符串转换为空值,并分隔 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))
看看这是否有帮助:返回 CSV - 将 CSV 文本转换为对象;通过 JSON
这是 2008 年 11 月发布的博客文章,其中包含 C# 代码以提供解决方案。
从博客文章的介绍中:
因为 Json 比 Xml 更容易读写。因此 CSV(逗号分隔值)比 Json 更容易读写。CSV 还具有 Excel 等工具,可以轻松使用和创建。因此,如果您想为下一个应用程序创建配置或数据文件,这里有一些将 CSV 转换为 JSON 到 POCO 对象的代码