https://github.com/skrtdev/json2
JSON2 converts json and arrays in structured classes
https://github.com/skrtdev/json2
class hacktoberfest json mapper php
Last synced: 5 months ago
JSON representation
JSON2 converts json and arrays in structured classes
- Host: GitHub
- URL: https://github.com/skrtdev/json2
- Owner: skrtdev
- License: mit
- Created: 2021-05-22T14:08:56.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-01T15:55:40.000Z (about 3 years ago)
- Last Synced: 2024-04-03T19:45:11.063Z (about 1 year ago)
- Topics: class, hacktoberfest, json, mapper, php
- Language: PHP
- Homepage:
- Size: 9.77 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON2
JSON2 converts json and arrays in structured classes
Full example
```php
user ??= json2_decode(api_call('users', $this->user_id), User::class);
}/**
* @return Comment[]
*/
public function getComments(): array
{
return $this->comments ??= json2_decode(api_call('posts', $this->id, 'comments'), Comment::class);
}
}class User{
protected int $id;
protected string $name;
protected string $username;
protected Address $address;
protected Company $company;
}class Address{
protected Location $geo;
}class Location{
protected float $lat;
protected float $lng;
}class Company{
protected string $name;
#[JSONProperty(json: 'catchPhrase')]
protected string $catch_phrase;
}class Comment{
#[JSONProperty(json: 'postId')]
protected string $post_id;
}var_dump($posts);
var_dump($posts[30]->getUser());
var_dump($posts[6]->getComments());
```