PHP code example of tc / json

1. Go to this page and download the library: Download tc/json 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/ */

    

tc / json example snippets




use Tc\Json\Json;
use Tc\Json\JsonException;

$data = [
    'foo' => 'bar',
];

$jsonString = Json::encode($data); // {"foo": "bar"}

$jsonDataObject = Json::decode($jsonString); // ( [foo] => bar )

$jsonDataArray = Json::decode($jsonString, true ); // ['foo' => 'bar']

$jsonData = Json::decode($jsonString, true); // ['foo' => 'bar']

try {
    Json::decode('{"foo"'); // invalid json string
} catch(JsonException $e) {
    echo $e->getMessage(); // description of error
}