Recommend this page to a friend! |
PHP implementation of json_encode/decode for PHP 7.2 and higher. This library works with and without quoted keys.
This package provides a simple encoder and decoder for JSON notation. It is intended for use with client-side Javascript applications that make use of HTTPRequest to perform server communication functions - data can be encoded into JSON notation for use in a client-side javascript, or decoded from incoming Javascript requests. JSON format is native to Javascript, and can be directly eval() with no further parsing overhead.
While this version doesn't have a better performance than json_encode() and json_decode() available as extension, but it has the next features:
Install the library using composer
composer require eftec/services_json
Include the dependency
use eftec\ServicesJson\Services_JSON;
include '../vendor/autoload.php';
See the folder examples for further examples
Include the file
use eftec\ServicesJson\Services_JSON;
include 'Services_JSON.php'; // or where is located the file.
Decode a JSON string into a stdclass or an associative array
$json='{"hello":{"a":2,"b":3},"world":[1,2,3,"aaa"]}';
var_dump(Services_JSON::decode($json)); // as stdclass
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array
It also works with unquoted keys
$json='{hello:{a:2,b:3},world:[1,2,3,"aaa","bbbb"]}'; // the keys are unquoted.
var_dump(Services_JSON::decode($json)); // as stdclass
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array
With the flag Services_JSON::DECODE_FIX_ROOT, it also works when the origin text misses [] and {} at the start of the code. Note, this feature is not foolproof, for example "[1,2,3],[4,5,6]" will not wrap as "[[1,2,3],[4,5,6]]"
{"a":222,"b:"ccc"} # normal json
"a":222,"b:"ccc" # json without the root parenthesis.
Services_JSON::decode('1,2,3',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT); // returns [1,2,3]
Services_JSON::decode('"k1":"v1", k2:2',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) // returns [ 'k1' => 'v1','k2'=>2]
> Note: DECODE_FIX_ROOT flag detects if the near character is ":" or ",". If the closest character is ":", then it returns > an object, otherwise it returns a list. If there is none, then it returns a list.
With the flag Services_JSON::DECODE_NO_QUOTE it also works when the string values are not quoted.
>Note: invisible characters at the beginner and at the end (tabs, line carriages) will be trimmed.
Services_JSON::decode('{"a":aaa,"b":bbbb,"c": aaaaa}'
, Services_JSON::GET_ARRAY | Services_JSON::DECODE_NO_QUOTE) // ['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa']
Encode transform a value (array, object, primitive value, etc.) into a json expression (a string)
$array=["hello"=>['a'=>2,'b'=>3],'world'=>[1,2,3,"aaa","bbb"]];
$obj=(object)$array;
var_dump(Services_JSON::encode($array)); // encode an associative array
var_dump(Services_JSON::encode($obj)); // encode an object
Classes of Jorge Castro | > | How to Implement a PHP to JSON String Conversion Without the Limitations of json_encode by Using Services_JSON | > | Download .zip .tar.gz | > | Support forum | > | Blog (1) | > | Latest changes |
|
Groups | Applications | Files |
Groups |
PHP 5 | Classes using PHP 5 specific features | View top rated classes |
Text processing | Manipulating and validating text data | View top rated classes |
Conversion | Unit and datatype conversion | View top rated classes |
Data types | Modeling and manipulating data types | View top rated classes |
Innovation Award |
November 2022 Nominee Vote |
JSON (JavaScript Object Notation) is a popular format for exchanging data structures between applications in many different languages. PHP provides built-in functions to encode and decode data structure values in JSON format. Currently, PHP does not support decoding objects encoded in JSON format with the object keys with double quotes. This package provides a workaround written in pure PHP for decoding objects with or without quotes in the object key names. This possibility overcomes the limitation of the current PHP versions. It allows the decoding processing of JSON data generated by applications written in other languages or components that do not use quotes to encode object key names. Manuel Lemos |
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Files | / | examples |
File | Role | Description |
---|---|---|
example_decode.php | Aux. | Auxiliary script |
example_decode_unquoted.php | Aux. | Auxiliary script |
example_encode.php | Aux. | Auxiliary script |
Download all files: services_json.tar.gz services_json.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|