JsonMapper 一个将内嵌JSON结构映射到PHP类上的库
最后更新于:2022-04-02 02:25:51
[TOC]
> [github](https://github.com/cweiske/jsonmapper)
## 安装
`composer require netresearch/jsonmapper`
## 使用
```
$json='{"name":"Sheldon Cooper","address":{"street":"2311 N. Los Robles Avenue","city":"Pasadena"}}';
class Contact
{
/**
* Full name
* @var string
*/
public $name;
/**
* @var Address
*/
public $address;
}
class Address
{
public $street;
public $city;
public function getGeoCoords()
{
echo "hello word:===".$this->city."===";
}
}
$mapper = new JsonMapper();
$contact = $mapper->map(json_decode($json), new Contact());
$contact->address->getGeoCoords();//hello word:===Pasadena===
```
';