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 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

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

// Encode options
$options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;
echo Json::encode($data, $options);

// 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();
}