json_decode ($json, $assoc = FALSE, $depth = 512, $options = 0)Options :This function takes four parameters as above and described below:
- json:contains the JSON string to decode. Works only with UTF-8 encoded strings.
- Assoc:is a boolean variable. If true, the returned objects will be converted to associative arrays.
- depth:indicates the recursion depth specified by the user.
- options: includes a bit mask JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING ,, JSON_THROW_ON_ERROR.
Example 1:
// Declare the json string
$json
=
’{" g ": 7," e ": 5," e ": 5," k ": 11," s ": 19}’
;
// Use the json_decode() function for
// decode the string
var_dump (json_decode (
$json
));
var_dump (json_decode (
$json
, true));
?>
Exit:object (stdClass ) # 1 (4) {["g"] = > int (7) ["e"] = > int (5) ["k"] = > int (11) ["s"] = > int (19)} array (4) {["g"] = > int (7) ["e"] = > int (5) ["k"] = > int (11) ["s"] = > int (19)}
Example 2:
// Declare the json string
$json
=
’{" engineer ": 7551119}’
;
// Use the json_decode() function for
// decode the string
$obj
= json_decode (
$json
);
// Show json object value
print
$obj
-> {
’engineer’
};
?>
Exit:7551119 Common mistakes when using the json_decode() function:
- The strings used are valid JavaScript, but not valid JSON.
- Name and the value must be enclosed in double quotes, single quotes are not allowed.
- Trailing commas are not allowed.
Link:http://php.net/manual/en/function.json-decode.php