JSON
suggest changeVersions
[{“Name”:“5.2”,“GroupName”:“PHP 5.x”},{“Name”:“5.3”,“GroupName”:“PHP 5.x”},{“Name”:“5.4”,“GroupName”:“PHP 5.x”},{“Name”:“5.5”,“GroupName”:“PHP 5.x”},{“Name”:“5.6”,“GroupName”:“PHP 5.x”},{“Name”:“7.0”,“GroupName”:“PHP 7.x”},{“Name”:“7.1”,“GroupName”:“PHP 7.x”}]
Introduction
JSON (JavaScript Object Notation) is a platform and language independent way of serializing objects into plaintext. Because it is often used on web and so is PHP, there is a basic extension for working with JSON in PHP.
Syntax
- string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
- mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
Parameters
Parameter | Details | ——— | —–– |
|json_encode| -| | value | The value being encoded. Can be any type except a resource. All string data must be UTF-8 encoded. | |options|Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR. The behaviour of these constants is described on the JSON constants page.| |depth|Set the maximum depth. Must be greater than zero.| | json_decode | -| | json | The json string being decoded. This function only works with UTF-8 encoded strings. | |assoc|Should function return associative array instead of objects.| |options|Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)|
Remarks
- json_decode handling of invalid JSON is very flaky, and it is very hard to reliably determine if the decoding succeeded, json_decode returns null for invalid input, even though null is also a perfectly valid object for JSON to decode to. To prevent such problems you should always call json_last_error** every time you use it.**