Php json decode
From w3cyberlearnings
Contents |
PHP function json_decode
This function decodes JSON data into array or object.
Syntax json_decode
- json_string: json data
- bool: True (make associative array), False
- dept: specifies recursive dept
- bitmask: Bitmask of JSON decode options, currently only support JSON_BIGINT_AS_STRING
json_decode(json_string, bool, dept, bitmask);
Example 1: make associative array
<?php $json_data ='{"name":"Christ","age":13}'; $object = json_decode($json_data,true); print_r( $object ); ?>
Output
Array ( [name] => Christ [age] => 13 )
Example 2: make object
<?php $json_data ='{"name":"Christ","age":13}'; $object = json_decode($json_data); print_r( $object ); ?>
Output
stdClass Object ( [name] => Christ [age] => 13 )
Example 3
<?php $json_data='{ "group1": { "name":"w3cb", "size":12, "leader":{ "name": [ {"fname":"bob","last":"kee"}, {"fname":"makr","last":"aul"} ] } }, "group2": { "name":"w3c2b", "size":12, "leader":{ "name": [ {"fname":"bob","last":"kee"}, {"fname":"makr","last":"aul"} ] } }, "group3": { "name":"w4cb", "size":11, "leader":{ "name": [ {"fname":"bob","last":"kee"}, {"fname":"makr","last":"aul"} ] } } }'; $object = json_decode($json_data); print_r( $object ); ?>
Output
stdClass Object ( [group1] => stdClass Object ( [name] => w3cb [size] => 12 [leader] => stdClass Object ( [name] => Array ( [0] => stdClass Object ( [fname] => bob [last] => kee ) [1] => stdClass Object ( [fname] => makr [last] => aul ) ) ) ) [group2] => stdClass Object ( [name] => w3c2b [size] => 12 [leader] => stdClass Object ( [name] => Array ( [0] => stdClass Object ( [fname] => bob [last] => kee ) [1] => stdClass Object ( [fname] => makr [last] => aul ) ) ) ) [group3] => stdClass Object ( [name] => w4cb [size] => 11 [leader] => stdClass Object ( [name] => Array ( [0] => stdClass Object ( [fname] => bob [last] => kee ) [1] => stdClass Object ( [fname] => makr [last] => aul ) ) ) ) )
Related Links
---json_encode--- json_decode--- json_last_error---