PHP code example of sgmendez / json

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

    

sgmendez / json example snippets


php
use Sgmendez\Json\Json;

$json = new Json();

try
{
    $arrayData = array('foo' => 'Foo', 'bar' => 'Bar');
    $jsonData = $json->encode($arrayData);
} 
catch (Exception $ex) 
{
    echo '[EXCEPTION] MSG: '.$ex->getMessage().' | FILE: '.$ex->getFile().': '.$ex->getLine()."\n";
}


php
use Sgmendez\Json\Json;

$json = new Json();

try
{
    $jsonData = '{"foo":"Foo","bar":"Bar"}';
    $dataArray = $json->decode($jsonData);
} 
catch (Exception $ex) 
{
    echo '[EXCEPTION] MSG: '.$ex->getMessage() .
         ' | FILE: '.$ex->getFile().': '.$ex->getLine()."\n";
}



php
use Sgmendez\Json\Json;

$json = new Json();

try
{
    $dataArray = $json->decodeFile('/path/to/file.json');
} 
catch (Exception $ex) 
{
    echo '[EXCEPTION] MSG: '.$ex->getMessage() .
         ' | FILE: '.$ex->getFile().': '.$ex->getLine()."\n";
}