mrbenosborne/json-unmarshal

一个用于将 JSON 数据解包到类属性的 PHP 8 包。

1.0.1 2021-01-12 14:08 UTC

This package is auto-updated.

Last update: 2024-09-28 23:25:45 UTC


README

一个用于将 JSON 数据解包到类属性的 PHP 包。

Build Latest Stable Version Latest Unstable Version License composer.lock

安装

通过 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);