ozdemirburak/json-csv 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ozdemirburak/json-csv

Composer 安装命令:

composer require ozdemirburak/json-csv

包简介

JSON to CSV and CSV to JSON converters in PHP.

README 文档

README

Latest Version on Packagist Software License Build Status Total Downloads

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 ozdemirburak/json-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 169.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 40
  • 点击次数: 6
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 40
  • Watchers: 1
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-26