PHP JSON
In this chapter, we will introduce how to use PHP to encode and decode JSON objects.
Environment Configuration
The JSON extension is built into PHP 5.2.0 and above.
JSON Functions
| Function | Description |
|---|---|
| json_encode | Encode a variable as JSON |
| json_decode | Decode a JSON string to a PHP variable |
| json_last_error | Return the last error occurred |
json_encode
PHP json_encode() is used to encode a variable as JSON. This function returns JSON data on success, or FALSE on failure.
Syntax
string json_encode ( $value [, $options = 0 ] )
Parameters
- value: The value to be encoded. This function only works with UTF-8 encoded data.
options: A bitmask composed 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, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
It should be noted that with the JSON_UNESCAPED_UNICODE option, if we don't want Chinese characters to be encoded, we can add this option.
Example
The following example demonstrates how to convert a PHP array to JSON format data:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
The output of the above code is:
{"a":1,"b":2,"c":3,"d":4,"e":5}
The following example demonstrates how to convert a PHP object to JSON format data:
Example
class Emp {
public $name = "";
public $hobbies = "";
public $birthdate = "";
}
$e = new Emp();
$e->name = "sachin";
$e->hobbies = "sports";
$e->birthdate = date('m/d/Y h:i:s a', "8/5/1974 12:20:03 p");
$e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974 12:20:03"));
echo json_encode($e);
?>
The output of the above code is:
{"name":"sachin","hobbies":"sports","birthdate":"08\/05\/1974 12:20:03 pm"}
Using the JSON_UNESCAPED_UNICODE option
$arr = array('example' => 'Rookie Tutorial', 'taobao' => 'Taobao');
echo json_encode($arr); //Encode Chinese
echo PHP_EOL; //Newline character
echo json_encode($arr, JSON_UNESCAPED_UNICODE); //Do not encode Chinese
?>
The output of the above code is:
{"example":"\u83dc\u9e1f\u6559\u7a0b","taobao":"\u6dd8\u5b9d\u7f51"}
{"example":"Example","taobao":"淘宝网"}
json_decode
The PHP json_decode() function is used to decode a JSON string and convert it to a PHP variable.
Syntax
mixed json_decode ($json_string [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
Parameters
json_string: The JSON string to be decoded. It must be UTF-8 encoded data.
assoc: When this parameter is TRUE, an array is returned; when FALSE, an object is returned.
depth: An integer parameter specifying the recursion depth.
options: A bitmask. Currently only JSON_BIGINT_AS_STRING is supported.
Example
The following example demonstrates how to decode JSON data:
Example
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
The output of the above code is:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
Other Extensions