How to use JSON in PHP, how to use Phpjson
Starting with version 5.2, PHP native provides the Json_encode () and Json_decode () functions, which are used for encoding, which is used for decoding.
Json_encode ()
This function is primarily used to convert arrays and objects into JSON format.
Copy the Code code as follows:
$arr = Array (' a ' = = ' A ', ' B ' = ' B ', ' c ' = ' C ', ' d ' = ' d ', ' e ' = ' e ');
echo Json_encode ($arr);
Output Result:
JSON accepts only utf-8 encoded characters, and Json_encode () parameters must be utf-8 encoded.
Class Person {public $name; public $age; public $height; function __construct ($name, $age, $height) { $this->name = $name; $this->age = $age; $this->height = $height; }
Output Result:
When a property in a class is a private variable, it is not output.
Json_decode ()
This function is used to convert the JSON text to the appropriate PHP data structure.
Copy the Code code as follows:
$json = ' {' A ': ' Hello ', ' B ': ' World ', ' C ': ' Zhangsan ', ' d ': ', ' E ': 170} ';
Var_dump (Json_decode ($json));
Output Result:
Typically, Json_decode () always returns a PHP object.
To an array:
Copy the Code code as follows:
$json = ' {' A ': ' Hello ', ' B ': ' World ', ' C ': ' Zhangsan ', ' d ': ', ' E ': 170} ';
Var_dump (Json_decode ($json, ture));
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/992544.html www.bkjia.com true http://www.bkjia.com/PHPjc/992544.html techarticle The use of JSON in PHP, Phpjson use method starting from the 5.2 version, PHP native provides Json_encode () and Json_decode () functions, the former for encoding, the latter for decoding. Json_encode () the ...