PHP code example of francerz / json-tools

1. Go to this page and download the library: Download francerz/json-tools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

francerz / json-tools example snippets


use Francerz\JsonTools\JsonMap;
use Francerz\JsonTools\JsonMappedInterface;

class Student implements JsonMappedInterface
{
    private $studentId;
    private $givenName;
    private $familyName;

    public function getJsonMaps()
    {
        return [
            new JsonMap('id', 'studentId'),
            new JsonMap('given_name', 'givenName'),
            new JsonMap('family_name', 'familyName')
        ];
    }
}

$json = '{"id":123,"given_name":"John","family_name":"Doe"}';

$student = \Francerz\JsonTools\JsonEncoder::decode($json, Student::class);

$json = '[' .
    '{"id":123,"given_name":"John","family_name":"Doe"},' .
    '{"id":321,"given_name":"Jane","family_name":"Smith"}' .
']';

$students = \Francerz\JsonTools\JsonEncoder::decode($json, Student::class);