mrbenosborne / json-unmarshal
一个用于将 JSON 数据解包到类属性的 PHP 8 包。
1.0.1
2021-01-12 14:08 UTC
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9
- psalm/plugin-phpunit: ^0.15.0
- vimeo/psalm: ^4.3
README
一个用于将 JSON 数据解包到类属性的 PHP 包。
安装
通过 composer 安装。
composer require mrbenosborne/json-unmarshal
示例
以下是一个 Flight 类的示例,完整的示例可以在 examples/ 文件夹中找到。
<?php use JSON\Attributes\JSON; use JSON\Unmarshal; include '../vendor/autoload.php'; include 'FlightRoute.php'; /** * Class Flight */ class Flight { #[JSON(field: 'airline')] public string $airlineName; #[JSON(field: 'aircraft.type')] public string $aircraftType; #[JSON(field: 'route', type: FlightRoute::class)] public array $route; } // Create a new flight class $flight = new Flight(); // Load our JSON data from file $jsonData = json_decode(file_get_contents('flight.json'), true); // Unmarshal JSON Unmarshal::decode($flight, $jsonData);