PHP code example of hampel / json

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

    

hampel / json example snippets




use Hampel\Json\Json;
use Hampel\Json\JsonException;

$data = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];

// Encode a variable as JSON:
echo Json::encode($data);

// Encode options - escape characters that are unsafe in HTML
$html = ['note' => '<a href="x">Tom & Jerry\'s</a>'];
$options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;

echo Json::encode($html);
// {"note":"<a href=\"x\">Tom & Jerry's<\/a>"}

echo Json::encode($html, $options);
// {"note":"\u003Ca href=\u0022x\u0022\u003ETom \u0026 Jerry\u0027s\u003C\/a\u003E"}

// Decode JSON:
print_r(Json::decode('{"a":1,"b":2,"c":3,"d":4,"e":5}'));

// Error handling
try {
    Json::decode('{"a":1,"b":2,"c":3,"d":4,"e":5'); // missing }
} catch (JsonException $e) {
    echo "Oops: " . $e->getMessage();
}