PHP JSON
PHP JSON
? ???? PHP ??? ???? JSON ??? ??? ? ????? ??? ?????.
?? ??
php5.2.0 ????? JSON ?? ??? ???? ????.
JSON ??
json_encode
PHP json_encode()? JSON ??? ??? ?????. ? ??? ????? ???? JSON ???? ????, ??? ??? ?????. ??.
??
string json_encode( $value [, $options = 0 ] )
????
·?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??? UTF-8? ???? ????? ?????.
· ??: ?? ??? ??? ???? ???: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
?
?? ???? ??? ?????. PHP ??? JSON ?? ???? ?????:
<?php $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?>
? ??? ?? ??? ??? ????:
{"a":1,"b":2,"c":3, "d ":4,"e":5}
?? ???? PHP ??? JSON ?? ???? ???? ??? ?????.
<?php 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); ?>
? ??? ?? ??? ??? ????.
{ "??":"??","??":"???","????":"1974? 8? 5? ?? 12:20:03"}
json_decode
PHP json_decode() ??? JSON ?? ???? ????? ?? PHP ??? ???? ? ?????.
??
?? json_decode($json [,$assoc = false [, $length = 512 [, $options = 0 ]]])
????
·???????? json_string: ???? JSON ???? UTF-8? ???? ????? ???.
·????????????? assoc: ? ????? TRUE?? ??? ????, FALSE?? ??? ?????.
· ??: ?? ??? ???? ??? ?? ??
· ??: ???? ???, ?? JSON_BIGINT_AS_STRING? ?????.
?
?? ?? JSON ???? ????? ??? ?????.
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); var_dump(json_decode($json, true)); ?>
? ??? ?? ??? ??? ????.
object(stdClass)# 1 (5) {
["a"] => int(1)
["b"] => int(2)
["c "] => ; int(3)
["d"] => int(4)
["e"] => int(5)
}
??(5) {
["a"] => int(1)
["b"] => ??>
["c"] => int(3) ["d"] => int(4) ["e"] => (5)}