PHP code example of webfiori / jsonx

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

    

webfiori / jsonx example snippets


//load the class "Json"
h/to/WebFiori/Json/Json.php'; // If manually installed

use WebFiori\Json\Json;

//initialize an object of the class Json
$j = new Json();

//add a number attribute
$j->addNumber('my-number', 34);

//add a boolean with 'false' as its value. 
$j->addBoolean('my-boolean', false);

//add a string
$j->addString('a-string', 'Hello, I\'m Json! I like "JSON". ');

header('content-type:application/json');

// Output the JSON string
echo $j;

use WebFiori\Json\Json;

$jsonObj = new Json([
    'first-name' => 'Ibrahim',
    'last-name' => 'BinAlshikh',
    'age' => 26,
    'is-married' => true,
    'mobile-number' => null
]);

echo $jsonObj;

use WebFiori\Json\Json;

// Set style and case in constructor
$json = new Json([], 'camel', 'lower');

// Add properties
$json->add('first-name', 'Ibrahim');
$json->add('last-name', 'BinAlshikh');

echo $json;

$json->setPropsStyle('snake', 'upper');
echo $json;

use WebFiori\Json\Json;

try {
    $jsonObj = Json::fromJsonFile('/path/to/file.json');
    
    // Access properties
    $value = $jsonObj->get('propertyName');
    
    echo $value;
} catch (\WebFiori\Json\JsonException $ex) {
    echo 'Error: ' . $ex->getMessage();
}

use WebFiori\Json\Json;
use WebFiori\Json\JsonI;

class Person implements JsonI {
    private $firstName;
    private $lastName;
    private $age;
    
    public function __construct($firstName, $lastName, $age) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->age = $age;
    }
    
    public function toJSON(): Json {
        $json = new Json();
        $json->addString('first-name', $this->firstName);
        $json->addString('last-name', $this->lastName);
        $json->addNumber('age', $this->age);
        
        return $json;
    }
}

$json = new Json();
$person = new Person('Ibrahim', 'BinAlshikh', 30);
$json->addObject('person', $person);

echo $json;

class User {
    private $username;
    private $email;
    
    public function __construct($username, $email) {
        $this->username = $username;
        $this->email = $email;
    }
    
    public function getUsername() {
        return $this->username;
    }
    
    public function getEmail() {
        return $this->email;
    }
}

$json = new Json();
$user = new User('ibrahimBin', '[email protected]');
$json->addObject('user', $user);

echo $json;

use WebFiori\Json\Json;

$jsonString = '{"name":"Ibrahim","age":30,"city":"Riyadh"}';

try {
    $jsonObj = Json::decode($jsonString);
    
    // Access properties
    echo $jsonObj->get('name'); // Outputs: Ibrahim
    echo $jsonObj->get('age');  // Outputs: 30
    echo $jsonObj->get('city'); // Outputs: Riyadh
} catch (\WebFiori\Json\JsonException $ex) {
    echo 'Error: ' . $ex->getMessage();
}

use WebFiori\Json\Json;

$json = new Json([
    'name' => 'Ibrahim',
    'age' => 30,
    'city' => 'Riyadh'
]);

try {
    $json->toJsonFile('data', '/path/to/directory', true);
    // This will create /path/to/directory/data.json
    
    echo 'File saved successfully!';
} catch (\WebFiori\Json\JsonException $ex) {
    echo 'Error: ' . $ex->getMessage();
}

use WebFiori\Json\Json;

$json = new Json();

// Simple array
$json->addArray('numbers', [1, 2, 3, 4, 5]);

// Array of objects
$json->addArray('users', [
    ['name' => 'Ibrahim', 'age' => 30],
    ['name' => 'Jane', 'age' => 25],
    ['name' => 'Bob', 'age' => 40]
]);

echo $json;

use WebFiori\Json\Json;

$json = new Json();

$json->addArray('data', [
    'name' => 'Ibrahim',
    'age' => 30,
    'skills' => ['PHP', 'JavaScript', 'Python']
], true); // true means represent as object

echo $json;

use WebFiori\Json\Json;

$json = new Json([
    'name' => 'Ibrahim',
    'age' => 30,
    'isEmployed' => true,
    'address' => [
        'city' => 'Riyadh',
        'country' => 'Saudi Arabia'
    ]
]);

// Output as JSONx
echo $json->toJSONxString();

use WebFiori\Json\Json;

try {
    // Attempt to decode invalid JSON
    $jsonObj = Json::decode('{invalid json}');
} catch (\WebFiori\Json\JsonException $ex) {
    echo 'Error code: ' . $ex->getCode() . "\n";
    echo 'Error message: ' . $ex->getMessage();
}