PHP JSON
json?? ??????
JSON(JavaScript Object Notation)? ?? ??? ??? ???? ?? ??? ?? ?????.
JSON? JavaScript ??? ??? ??? ??? ???? ??? ? ???, ???? ?? ?? ?? ????? ? ??????? ??? ?????? ????? ??? ??? ? ????. ? ???? ?? ??? ???? JavaScript? ?? ?? ??? ? ??? JSON? "??/? ?"?? ? ??? ??? ??? ? ????. ?? ??, ??? ?? ? ??? ?? ??? ??? ??? ??? ? ????.
? ?? ??? ?? ?? ???? ???? ? ??? ????.
????
php5.2.0 ????? JSON ?? ??? ???? ????.
JSON ??
json_encode
PHP json_encode ()? ??? JSON?? ????? ? ?????. ? ??? ????? ???? JSON ???? ????, ??? ??? FALSE? ?????.
??
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); ?>
?? ?? ? ??? ??? ????.
{"name":"sachin","hobbies":"sports","birthdate":"08/05/1974 12:20:03 pm"}
json_decode
PHP json_decode() ??? JSON ?? ???? ????? ?? PHP ??? ???? ? ?????.
??
?? json_decode($json [,$assoc = false [, $length = 512 [, $options = 0 ]]])
<數(shù)>????
· JSON_STRING: ???? JSON ???? UTF-8 ?? ????? ???.· assoc: ? ????? true?? ???? ?????. return. ??, 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"] =>
["b"] => int(2) ["c"] => int(3) ["d"] => ) ["e"] => int(5)} ??(5) { [ "a" ] => int(1) ["b"] => int(2) ["c"] =>["d"] => int(4)
["e"] => int(5)
}
PHP? Json ?? ??
- ?? ???? ??(", ")? ?????.
- ??? ?????. ??(":")?? ?????. - ?? ???? ???(??)? ???("[]")? ?????.
- ??? ???(??)? ???("{})? ?????. ")
?? ??:
"???? ??? 16,800?? ??????, ?? ??? 1,600? ????. ???? ??? 6,400?? ???????. ?? ??? 1,800?????." ??? ?? json ???? ?????.
[
{"City":"Beijing","Area":16800," Population":1600},
{"City" ":"Shanghai","Area":6400,"Population":1800}
]
???? ?? PHP? json
??? json_encode
json_decode ??
1?? ??? json ??? ???? ??<?php
$arr_1 = array();
$arr_1['username'] = 'lisi';
$arr_1['age'] = 20;
echo json_encode($arr_1);//{"username":"lisi","age":20}
?>
<?php
$arr_2 = array();
// 三維數(shù)組
$arr_2['member']['lisi']['job'] = "worker";
$arr_2['member']['lisi']['age'] = 30;
$arr_2['member']['wangwu']['job'] = "student";
$arr_2['member']['wangwu']['age'] = 10;
echo json_encode($arr_2);
//{"member":{"lisi":{"job":"worker","age":30},"wangwu":{"job":"student","age":10}}}
?>
??? ??? ?? ?? json ???? ??, ?? ??? ??, ?? ??? ?? ? ?
<?php class Person{ public $name = "public name"; protected $ptName = "protected name"; private $pName = "private name"; public function sayName(){ return $this->name; } } $person1 = new Person(); echo json_encode($person1);//{"name":"public name"} ?>
json ??? ??? ?? ???? ??
<?php
$jsonStr = '{"key1":"value1","key2":"value2"}';
print_r(json_decode($jsonStr,false));//stdClass Object ( [key1] => value1 [key2] => value2
?>
<?php
$jsonStr = '{"key1":"value1","key2":"value2"}';
print_r(json_decode($jsonStr,true));//Array ( [key1] => value1 [key2] => value2 )
?>
json_decode($jsonStr ,true); ? ?? ????? true?? ??? ?? ???? ?????. ?????
??? ?????.