PHP JSON
? ???? PHP ??? ???? JSON ??? ??? ? ????? ??? ?????.
????
php5.2.0 ????? JSON ?? ??? ???? ????.
JSON ??
?????????????????????????????????????? ??>json_encode JSON?? ?? ???
json_decode JSON ??? ???? ????? PHP ??? ??
json_last_error ??? ??? ?? ??
1) ?? ???? ??(", ")? ?????.
2) ??? ??(":")?? ?????.3) ?? ???? ??(??)? ???("[]")? ?????.
4) ??? ???(??)? ???("{}")? ?????. json_encodePHP json_encode()? JSON ??? ??? ?????. ? ??? ????? ???? JSON ???? ????, ??? ??? FALSE? ?????. ??
string json_encode ( $value [, $options = 0 ] )
????
1. value: ???? ????. ? ??? UTF-8? ???? ????? ?????. 2. ??: ?? ??? ??? ???? ???: 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 $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); ?>
? ?? ?? ?? ??? ??? ????.
{"name":"sachin","hobbies":"sports","birthdate":"08/05/1974 12:20:03 pm"}
json_decode
???? json_decode($json [,$assoc = false [, $length = 512 [, $options = 0 ]]])
????
1. json_string: ???? JSON ???, UTF-8? ???? ????? ???2. ? ????? TRUE?? ??? ?????. , FALSE? ?? ??? ?????.
4. ??: ???? ???, ?? 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"] => (3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
[ "a "] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] = > int(4)
["e"] => int(5)
}