PHP code example of braincrafted / json

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

    

braincrafted / json example snippets


use Braincrafted\Json\Json;
use Braincrafted\Json\JsonDecodeException;

// Encode a variable as JSON:
echo Json::encode(array('name' => 'Bilbo Baggins'));

// Decode JSON:
print_r(Json::decode('{"name": "Bilbo Baggins"}'));

// Decode JSON into an array:
print_r(Json::decode('{"name": "Bilbo Baggins"}', Json::DECODE_ASSOC));

// Error handling
try {
    Json::decode('{"name": "Bilbo Baggins"'); // missing }
} catch (JsonDecodeException $e) {
    echo sprintf("Could not decode JSON.\nReason: %s", $e->getMessage());
}