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;
// 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;
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;