yadakhov/json

一个简单的 PHP 处理 JSON 的包装类。

v2.0.1 2016-12-31 15:41 UTC

This package is auto-updated.

Last update: 2024-09-21 19:55:26 UTC


README

Latest Stable Version License Build Status

一个用于处理 Json 的简单包装类。

在 PHP 中将 json 作为对象处理。提供了简单的 API,使用点符号进行字段访问。

使用 get() 或 set() 访问 json 结构中的任何字段。

require __DIR__.'/vendor/autoload.php';
use Yadakhov\Json;

$json = new Json(['status' => 'success', 'developer' => ['name' => 'Yada Khov']]);
echo $json;  // {"status":"success","developer":{"name":"Yada Khov"}}

$json->set('status', 'winning');
echo $json;  // {"status":"winning","developer":{"name":"Yada Khov"}}

安装

使用 Composer

$ composer require yadakhov/json

传统 PHP

// download src/Json.php to your code folder..
require_once '/path/to/Json.php';

use Yadakhov/Json;

$json = new Json();

使用:构造函数

可以接受数组或 JSON 编码的字符串。

// There are 2 ways to instantiation a new Json object

// 1.
$json1 = new Json('{"status":"success"}');

// 2
$json2 = new Json(['status' => 'success']);

API 函数

$json->get('dot.notation') - get a field
$json->set('dot.notation', $value) - set a field
$json->toString() - return the Json object as a string
$json->toStringDot() - return the dot notation of the structure.
$json->toStringPretty() - json pretty print
$json->toArray() - return the array representation.

设计决策

内部,JSON 被存储为 PHP 数组。这允许我们使用点符号。

JSON 编码字符串的左侧需要使用双引号。

{
    "status": "success"
}

依赖项

PHP 5.4 用于短数组语法。

此包使用 illuminate/support 来处理数组和字符串助手。

运行测试

./vendor/bin/phpunit