phpdocumentor/json-path

使用 JSON 路径表达式访问 PHP 对象

0.1.0 2024-05-24 12:05 UTC

This package is auto-updated.

Last update: 2024-09-11 16:49:49 UTC


README

JSON-path 是一个简单的 JSON 路径解析和评估器,适用于 PHP。它基于 Goessner 的 JSONPath 实现(http://goessner.net/articles/JsonPath/)。此代码允许您解析 json 路径并在 PHP 对象上评估它们。这使得它成为 PHP 对象结构的查询语言。

可能不是查询 PHP 对象最快的方法,但因为是存储为普通字符串的路径,所以在配置文件或数据库中使用它们很方便。这使得它成为需要根据用户输入查询 PHP 对象结构的工具的好选择。

安装

推荐通过 Composer 安装 JSON-path。

  composer require phpdocumentor/json-path

用法

$parser = \phpDocumentor\JsonPath\Parser::createInstance();
$query = $parser->parse('.store.book[*].author');

$executor = new \phpDocumentor\JsonPath\Executor();
foreach ($executor->execute($query, $json) as $result) {
    var_dump($result);
}