webfiori / jsonx
PHP library for creating well-formatted JSON strings.
Fund package maintenance!
paypal.me/IbrahimBinAlshikh
Installs: 44 091
Dependents: 6
Suggesters: 0
Security: 0
Stars: 7
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-12-23 09:31:59 UTC
README
A helper class library for creating JSON or JSONx strings in PHP. It can be used to create well-formatted json strings from any variable type (strings, numbers, boolean arrays and even objects).
Content
- What is JSON?
- Features
- Supported PHP Versions
- Installation
- Code Samples
- Basic Usage
- Converting Properties Case
- Reading From Files
- Working With Objects
- Decoding JSON String
- Storing Output
- Working With Arrays
- [JSONx](#jsonx]
What is JSON?
According to json.org, JSON is a data exchange format which is based partially on JavaScript. It is easy for humans to read and for machines to understand. JSON data is represented as pairs of keys and values.
Features
- Support fo creating well formatted JSON.
- Support for creating JSONx.
- Ability to decode JSON strings and convert them to
Json
objects. - Ability to read JSON files and map JSON values to PHP data types.
- Ability to manipulate JSON properties as needed.
Supported PHP Versions
Installation
If you are using composer to manage your dependencies, then it is possible to install the library by including the entry "webfiori/jsonx":"*"
in the require
section of your composer.json
file to install the latest release.
Another way to include the library is by going to releases and download the latest release and extract compressed file content and add them to your include directory.
Basic Usage
The process of using the classes is very simple. What you have to do is the following steps:
- Import (or include) the class
Json
. - Create an instance of the class.
- Add data as needed.
- Output the object using
echo
command or any similar one.
For more information and advanced use cases, check here.
Example
The following code shows a very simple usage example.
//load the class "Json" require_once 'Json.php'; 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'); /* send back the generated json string. The output of the code will be like that: { "my-number":34, "my-boolean":false, "my-number":"Hello, I'm Json! I like \"json\". ", } */ echo $j;
Following example shows how data can be added directly using the constructure.
$jsonObj = new Json([ 'first-name' => 'Ibrahim', 'last-name' => 'BinAlshikh', 'age' => 26, 'is-married' => true, 'mobile-number' => null ]);
The JSON output of this code will be the following:
{ "first-name":"Ibrahim", "last-name":"BinAlshikh", "age":26, "is-married":true, "mobile-number":null }