sobernt / json-object
1.0.7
2019-12-17 03:17 UTC
Requires
- php: ^7.2
- ext-json: *
This package is not auto-updated.
Last update: 2024-10-02 01:59:53 UTC
README
安装
通过 composer 安装此包
composer require sobernt/json-object
用法
用最小参数初始化对象
$obj = new sobernt\JsonObject\JsonObject("{
\"testkey\":\"testval\",
\"testarray\":[
\"testsimplearrayval1\",
\"testsimplearrayval2\"
],
\"testcompositearray\":[
\"testcompositearrayval1\",
{
\"testcompositearray2key\": \"testcompositearray2value\"
}
],
\"testobject\":{
\"testobjectkey\": \"testobjectval\",
\"testobjectintkey\": \"1\"
}
}");
完整构造函数
__construct($json,$max_depth=255)
将 $max_depth = 500
设置为重新配置对象最大递归级别 = 500
如果 JSON 无法解析,则抛出 JsonException
您可以将此用作默认对象 $obj->testobject
此示例返回数组
$obj->testarray
此示例返回对象
$obj->testcompositearray[1]
如果您调用在 JSON 中未找到的对象,则抛出 InvalidArgumentException
您可以设置我们的属性 [字符串或 JsonObject]
$obj->prop="testprop"
此示例从树中返回值
$obj->filter("testcompositearray[1].testcompositearray2key")
您可以设置您自己的格式化程序用于原始对象
$this->setFormatter(function(string $data,string $name=null)
{
..
return $data;
}
)
格式化程序必须有两个导入参数:data 和 name。如果输入不可调用,则抛出 InvalidArgumentException
。