thevenrex/

json-to-class

Json 类初始化器

v1.0 2024-09-30 22:18 UTC

This package is auto-updated.

Last update: 2024-09-30 22:29:16 UTC


README

Json 解码为类

第一步

use Thevenrex\JsonToClass\JsonToClassDecoder;

用法

创建一个公共方法用于在 json 中使用的类

class User {
	public int $id;
	public string $name;
	public string $username;
}

使用原始 json 创建 JSON 类的新实例

$json = '{
    "id": 1,
    "name": "John Doe",
    "username": "johndoe"
}';
$decoder = new JsonToClassDecoder($rawJson);

解码内容

$user = new User();
$decoder->decode($user);

属性被 json 数据更改

var_dump($user);

示例输出

class User#3 (3) {
  public int $id =>
  int(1)
  public string $name =>
  string(5) "John Doe"
  public string $username =>
  string(10) "johndoe"
}