An open API service indexing awesome lists of open source software.

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

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());
```