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.
$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 ; $this $age ; $this $height ; } } $obj New person ("Zhangsan", 20,100); $foo _json = Json_encode ($obj); Echo $foo _json;
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.
$json = ' {' A ': ' Hello ', ' B ': ' World ', ' C ': ' Zhangsan ', ' d ': ', ' e ': "; Var_dump (Json_decode ($json));
Output Result:
Typically, Json_decode () always returns a PHP object.
To an array:
$json = ' {' A ': ' Hello ', ' B ': ' World ', ' C ': ' Zhangsan ', ' d ': ', ' e ': "; Var_dump (Json_decode ($json, ture));
Reprint Please specify source: http://www.cnblogs.com/yydcdut/p/3751141.html
http://www.bkjia.com/PHPjc/776511.html www.bkjia.com true http://www.bkjia.com/PHPjc/776511.html techarticle 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 to ...