2021年10月25日
-
$arr = Array( 'one' , 'two' , 'three' ); echo json_encode( $arr ); |
1 | [ "one" , "two" , "three" ] |
1 2 3 | $arr = Array( '1' => 'one' , '2' => 'two' , '3' => 'three' ); echo json_encode( $arr ); |
1 | { "1" : "one" , "2" : "two" , "3" : "three" } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Foo { const ERROR_CODE = '404' ; public $public_ex = 'this is public' ; private $private_ex = 'this is private!' ; protected $protected_ex = 'this should be protected' ; public function getErrorCode() { return self::ERROR_CODE; } } |
1 2 3 4 5 | $foo = new Foo; $foo_json = json_encode( $foo ); echo $foo_json ; |
1 | { "public_ex" : "this is public" } |
1 2 3 | $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ; var_dump(json_decode( $json )); |
1 2 3 4 5 6 7 8 9 10 | object(stdClass)#1 (5) { [ "a" ] => int(1) [ "b" ] => int(2) [ "c" ] => int(3) [ "d" ] => int(4) [ "e" ] => int(5) } |
1 2 3 | $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ; var_dump(json_decode( $json ,true)); |
1 2 3 4 5 6 7 8 9 10 | array (5) { [ "a" ] => int(1) [ "b" ] => int(2) [ "c" ] => int(3) [ "d" ] => int(4) [ "e" ] => int(5) } |
1 2 3 4 5 | $bad_json = "{ 'bar': 'baz' }" ; $bad_json = '{ bar: "baz" }' ; $bad_json = '{ "bar": "baz", }' ; |
1 | var_dump(json_decode( "Hello World" )); //null |