VictorAp93/readjson

Read Json 使得读取和处理 JSON 文件变得容易。

2.0.0 2022-04-02 14:35 UTC

This package is auto-updated.

Last update: 2024-09-09 18:34:41 UTC


README

Software License Latest Version Total Downloads

Read Json 使得读取和处理 JSON 文件变得容易。

安装

推荐通过 Composer 安装此包。

composer require victorap93/readjson

如何使用

假设你有一个 .json 文件 ./readjson/stores.json,内容如下。

{
    "stores": [
        {
            "name": "store one",
            "products": [
                {
                    "name": "product one",
                    "amount": 10
                },
                {
                    "name": "product two",
                    "amount": 20
                }
            ]
        },
        {
            "name": "store two",
            "products": [
                {
                    "name": "product two",
                    "amount": 22
                },
                {
                    "name": "product three",
                    "amount": 33
                }
            ]
        }
    ]
}

同一文件夹中还有一个 .php 文件 ./readjson/index.php,以下示例展示了不同的访问 .json 文件的方法。

use \victorap93\ReadJson;

$json_path = "./stores.json";

// Get all json content.
$ReadJson = new ReadJson($json_path);
$json_object_value1 = $ReadJson->getJsonObject();

// Accessing the obtained object.
$json_object_value2 = $json_object_value1->stores[0]->products;

// Get a specified json content position in instance of class.
$ReadJson = new ReadJson($json_path, ['stores', 0, 'products']);
$json_object_value3 = $ReadJson->getJsonObject();

// Get a specified json content position in method call.
$ReadJson = new ReadJson($json_path, ['stores', 0]);
$json_object_value4 = $ReadJson->getJsonObject(['products']);

// Get a specified json content position with object.
$json_object_value5 = $ReadJson->accessRecursiveKeys($json_object_value4, [0, 'name']);

帮助和文档

许可证

Read Json 在 MIT 许可证 (MIT) 下提供。请参阅 许可证文件 了解更多信息。