PHP json_decode() İşlevi
Örnek
JSON verilerini bir PHP değişkeninde depolayın ve ardından kodunu bir PHP nesnesine dönüştürün:
<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
var_dump(json_decode($jsonobj));
?>
Tanım ve Kullanım
json_decode() işlevi, bir JSON nesnesinin kodunu çözmek veya bir PHP nesnesine dönüştürmek için kullanılır.
Sözdizimi
json_decode(string, assoc, depth, options)
Parametre Değerleri
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) |
Teknik detaylar
Geri dönüş değeri: | Uygun PHP türünde JSON'da kodlanmış değeri döndürür. JSON nesnesinin kodu çözülemezse, NULL döndürür. |
---|---|
PHP Sürümü: | 5.2+ |
PHP Değişiklik Günlüğü: | PHP 7.3: JSON_THROWN_ON_ERROR seçeneği eklendi PHP 7.2: JSON_INVALID_UTF8_IGNORE ve JSON_INVALID_UTF8_SUBSTITUTE seçenekleri eklendi PHP 5.4: JSON_BIGINT_AS_STRING ve JSON_OBJECT_AS_ARRAY seçenekleri eklendi PHP 5 parametresi eklendi: PHP 5 parametresi eklendi: |
Daha fazla örnek
Örnek
JSON verilerini bir PHP değişkeninde depolayın ve ardından bir PHP ilişkisel dizisine dönüştürün:
<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
var_dump(json_decode($jsonobj,
true));
?>
Örnek
PHP nesnesinden değerlere nasıl erişilir:
<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
$obj = json_decode($jsonobj);
echo $obj->Peter;
echo $obj->Ben;
echo $obj->Joe;
?>
Örnek
PHP ilişkisel dizisindeki değerlere nasıl erişilir:
<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
$arr = json_decode($jsonobj, true);
echo $arr["Peter"];
echo $arr["Ben"];
echo $arr["Joe"];
?>
❮ PHP JSON Referansı