ozdemirburak/json-csv
Composer 安装命令:
composer require ozdemirburak/json-csv
包简介
JSON to CSV and CSV to JSON converters in PHP.
README 文档
README
The most basic CSV to JSON and JSON to CSV converter library in PHP without any dependencies.
Install
Via Composer
composer require ozdemirburak/json-csv
Note: Requires PHP 8.1 or higher with JSON extension
Usage
JSON to CSV Converter
use OzdemirBurak\JsonCsv\File\Json; // JSON to CSV $json = new Json(__DIR__ . '/file.json'); // To convert JSON to CSV string $csvString = $json->convert(); // To set a conversion option then convert JSON to CSV and save $json->setConversionKey('utf8_encoding', true); $json->convertAndSave(__DIR__ . '/file.csv'); // To convert JSON to CSV and force download on browser $json->convertAndDownload();
You can also convert directly from a JSON string using the fromString method.
$csvString = (new Json())->fromString('{"name": "John", "age": 30}')->convert();
Assume that the input JSON is something like below.
[
{
"name": {
"common": "Türkiye",
"official": "Republic of Türkiye",
"native": "Türkiye"
},
"area": 783562,
"latlng": [39, 35]
},
{
"name": {
"common": "Japan",
"official": "Japan",
"native": "日本"
},
"area": 377975,
"latlng": [36, 138]
}
]
After the conversion, the resulting CSV data will look like below.
| name_common | name_official | name_native | area | latlng_0 | latlng_1 |
|---|---|---|---|---|---|
| Türkiye | Republic of Türkiye | Türkiye | 783562 | 39 | 35 |
| Japan | Japan | 日本 | 377975 | 36 | 138 |
JSON Conversion Options
| Option | Type | Default | Description |
|---|---|---|---|
delimiter |
string | , |
CSV field delimiter |
enclosure |
string | " |
CSV field enclosure character |
escape |
string | \ |
CSV escape character |
join |
string | _ |
Character used to join nested keys |
null |
mixed | null |
Value to use for null/missing fields |
utf8_encoding |
bool | false |
Add UTF-8 BOM to output |
CSV to JSON Converter
use OzdemirBurak\JsonCsv\File\Csv; // CSV to JSON $csv = new Csv(__DIR__ . '/file.csv'); $csv->setConversionKey('options', JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); // To convert CSV to JSON string $jsonString = $csv->convert(); // To convert CSV to JSON and save $csv->convertAndSave(__DIR__ . '/file.json'); // To convert CSV to JSON and force download on browser $csv->convertAndDownload();
You can also convert directly from a CSV string using the fromString method.
$jsonString = (new Csv())->fromString("name,age\nJohn,30\n")->convert();
Assume that the input CSV file is something like below.
| SepalLength | SepalWidth | PetalLength | PetalWidth | Name |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
| 7.0 | 3.2 | 4.7 | 1.4 | Iris-versicolor |
| 6.3 | 3.3 | 6.0 | 2.5 | Iris-virginica |
After the conversion, the resulting JSON data will look like below.
[
{
"SepalLength": "5.1",
"SepalWidth": "3.5",
"PetalLength": "1.4",
"PetalWidth": "0.2",
"Name": "Iris-setosa"
},
{
"SepalLength": "7.0",
"SepalWidth": "3.2",
"PetalLength": "4.7",
"PetalWidth": "1.4",
"Name": "Iris-versicolor"
},
{
"SepalLength": "6.3",
"SepalWidth": "3.3",
"PetalLength": "6.0",
"PetalWidth": "2.5",
"Name": "Iris-virginica"
}
]
CSV Conversion Options
| Option | Type | Default | Description |
|---|---|---|---|
delimiter |
string | , |
CSV field delimiter |
enclosure |
string | " |
CSV field enclosure character |
escape |
string | \ |
CSV escape character |
join |
string | _ |
Character used to split nested keys |
numeric |
bool | false |
Set to true to convert numeric strings to numbers |
options |
int | 0 |
JSON encode flags (e.g., JSON_PRETTY_PRINT) |
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
ozdemirburak/json-csv 适用场景与选型建议
ozdemirburak/json-csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 169.76k 次下载、GitHub Stars 达 40, 最近一次更新时间为 2018 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「csv」 「json2csv」 「json to csv」 「csv to json」 「csv2json」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ozdemirburak/json-csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ozdemirburak/json-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ozdemirburak/json-csv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
Keboola JSON to CSV parser
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Keboola JSON to CSV parser
laravel facade to read/write csv file
统计信息
- 总下载量: 169.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 40
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-26