PHP Beginner's Introduction to JSON
1. Environment configuration
In php5.2.0 and above versions, JSON extension has been built-in
2. JOSN () function
json_encode Perform the variable json encoding # JSON_DECODE to decod in json format, convert it to PHP variables json_last_error Return to return The last error that occurredjson_encode
is used to JSON encode the variable. If the function is successfully executed, it returns JSON data, otherwise it returns FALSE Syntax: string json_encode ( $value [, $options = 0 ] )Parameters:
value: The value to be encoded. This function is only valid for UTF-8 encoded data. options: Binary mask consisting of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT## Example 1:How to convert a PHP array into json data
1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?>
The following example demonstrates how to convert a PHP object into JSON format data:
name = "sachin"; $e->hobbies = "sports"; $e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974 12:20:03")); echo json_encode($e); ?>
json_decode
PHP json_decode() function is used to decode JSON format strings and convert them into PHP variables.
Syntax
mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]] ])
Parameters
json_string: JSON string to be decoded, must be UTF-8 encoded data
assoc: When this parameter is TRUE, an array will be returned, and when FALSE, an object will be returned.
depth: Integer type parameter, which specifies the recursion depth
options: Binary mask, currently only JSON_BIGINT_AS_STRING is supported.
Example
The following example demonstrates how to decode JSON data:
"; var_dump(json_decode($json)); var_dump(json_decode($json, true)); echo ""; ?>