【问题标题】:Converting CSV file with column seperator to JSON将带有列分隔符的 CSV 文件转换为 JSON
【发布时间】:2015-11-30 08:46:31
【问题描述】:

我有一个 CSV 文件 (my_data.csv),我想将其转换为 JSON 对象。 与列“;”它给了我错误的结果,用逗号“,”它工作正常。 但我不想编辑 csv 文件。而且我不想使用 str_replace 因为 csv 文件很大并且需要更长的时间。 如何更改我的转换函数以生成正确的 JSON 格式? 这是我的 CSV 文件:

"Timestamp";"Longitude";"Latitude";"Client";"Price"
"2015-08-01 05:10:13";10.714069;48.031952;"test1";17.2
"2015-08-01 05:10:13";10.714069;48.031952;"test2";17.2
"2015-08-01 05:10:13";10.714069;48.031952;"test3";17.2
"2015-08-01 05:10:13";10.714069;48.031952;"test4";17.2

这是我的 PHP 代码

$file="data/my_file.csv";
$csv= file_get_contents($file);
$rows = explode("\n", trim($csv));
$data = array_slice($rows, 1);
$keys = array_fill(0, count($data),$rows[0] );
$json = array_map(function ($row, $key) {
    return array_combine(str_getcsv($key), str_getcsv($row));
}, $data, $keys);
$data=json_encode($json);
echo $data;

我明白了:

    [{"Timestamp;\"Longitude\";\"Latitude\";\"Client\";\"Price\"":"2015-08-01 05:10:13;10.714069;48.031952;\"test1\";17.2"},
{"Timestamp;\"Longitude\";\"Latitude\";\"Client\";\"Price\"":"2015-08-01 05:10:13;10.714069;48.031952;\"test2\";17.2"},
{"Timestamp;\"Longitude\";\"Latitude\";\"Client\";\"Price\"":"2015-08-01 05:10:13;10.714069;48.031952;\"test3\";17.2"},
{"Timestamp;\"Longitude\";\"Latitude\";\"Client\";\"Price\"":"2015-08-01 05:10:13;10.714069;48.031952;\"test4\";17.2"}]

应该是这样的:

[{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test1","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test2","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test3","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test4","Price":"17.2"}]

【问题讨论】:

  • 您正在从文件中读取 .csv,在这种情况下,您可以使用 fgetcsv() 删除代码中的一些文件读取/拆分样板。甚至是SplFileObject(),它是一个可遍历的文件对象,您可以将其视为.csv。

标签: php json csv


【解决方案1】:

你必须改变

return array_combine(str_getcsv($key), str_getcsv($row));

return array_combine(str_getcsv($key, ';'), str_getcsv($row, ';'));

因为参数$delimiter的默认值为',':

array str_getcsv ( string $input [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\" ]]] )

【讨论】:

  • 非常感谢,你拯救了我的一天 :)
  • 太棒了!请“接受”我的回答,如果它解决了您的问题,以便其他用户可以看到它是正确的答案 ;-) - stackoverflow.com/help/someone-answers
【解决方案2】:

替代解决方案:

<?php
$csvString = file_get_contents('/path/to/data.csv');
$csvRows = str_getcsv($csvString, "\n"); // split new lines, gives you rows
$rows = array();
foreach($csvRows as $row) {
    $rows[] = str_getcsv($row, ";"); // parse the rows
}

// remove the first row. its the CSV column heading line. keep keys for reuse.
$itemKeys = array_shift($rows);

// run over all rows and combine keys with values, to get a named array
foreach($rows as $rowKeys => $rowValues) {
    $array[] = array_combine($itemKeys, $rowValues);
}

echo json_encode($array);

结果:

[{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test1","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test2","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test3","Price":"17.2"},
{"Timestamp":"2015-08-01 05:10:13","Longitude":"10.714069","Latitude":"48.031952","Client":"test4","Price":"17.2"}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2021-05-22
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多