PHP json_decode() Funktion
Beispiel
Speichern Sie JSON-Daten in einer PHP-Variablen und decodieren Sie sie dann in ein PHP-Objekt:
<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
var_dump(json_decode($jsonobj));
?>
Definition und Verwendung
Die Funktion json_decode() wird zum Decodieren oder Konvertieren eines JSON-Objekts in ein PHP-Objekt verwendet.
Syntax
json_decode(string, assoc, depth, options)
Parameterwerte
Parameter | Description |
---|---|
string | Required. Specifies the value to be decoded |
assoc | Optional. Specifies a Boolean value. When set to true, the returned object will be converted into an associative array. When set to false, it returns an object. False is default |
depth | Optional. Specifies the recursion depth. Default recursion depth is 512 |
options | Optional. Specifies a bitmask (JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR) |
Technische Details
Rückgabewert: | Gibt den in JSON codierten Wert im entsprechenden PHP-Typ zurück. Wenn das JSON-Objekt nicht dekodiert werden kann, gibt es NULL zurück |
---|---|
PHP-Version: | 5.2+ |
PHP-Änderungsprotokoll: | PHP 7.3: Option JSON_THROWN_ON_ERROR hinzugefügt PHP 7.2: Optionen JSON_INVALID_UTF8_IGNORE und JSON_INVALID_UTF8_SUBSTITUTE hinzugefügt PHP 5.4: Optionen JSON_BIGINT_AS_STRING und JSON_OBJECT_AS_ARRAY hinzugefügt PHP 5.4: Optionsparameter hinzugefügt PHP 5.3: Tiefenparameter hinzugefügt |
Mehr Beispiele
Beispiel
Speichern Sie JSON-Daten in einer PHP-Variablen und decodieren Sie sie dann in ein assoziatives PHP-Array:
<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
var_dump(json_decode($jsonobj,
true));
?>
Beispiel
So greifen Sie auf die Werte aus dem PHP-Objekt zu:
<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
$obj = json_decode($jsonobj);
echo $obj->Peter;
echo $obj->Ben;
echo $obj->Joe;
?>
Beispiel
So greifen Sie auf die Werte aus dem assoziativen PHP-Array zu:
<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
$arr = json_decode($jsonobj, true);
echo $arr["Peter"];
echo $arr["Ben"];
echo $arr["Joe"];
?>
❮ PHP-JSON-Referenz