webfiori/jsonx

PHP library for creating well-formatted JSON strings.

Maintainers

Details

github.com/WebFiori/json

Source

Issues

Fund package maintenance!
paypal.me/IbrahimBinAlshikh

v3.3.0 2023-08-03 07: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).

badge.svg?branch=master 68747470733a2f2f636f6465636f762e696f2f67682f57656246696f72692f6a736f6e2f6272616e63682f6d61737465722f67726170682f62616467652e737667 68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d57656246696f72695f6a736f6e266d65747269633d616c6572745f737461747573 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77656266696f72692f6a736f6e783f636f6c6f723d6c696768742d677265656e

Content

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

Build Status
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master

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
}