W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
json_decode是php5.2.0之后新增的一個PHP內(nèi)置函數(shù),其作用是對JSON格式的字符串進(jìn)行編碼.那么這個函數(shù)該如何使用呢?
json_decode的語法規(guī)則: ?json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
json_decode接受一個JSON格式的字符串并且把它轉(zhuǎn)換為PHP變量 ,當(dāng)該參數(shù)$assoc為TRUE時,將返回array,否則返回object。
JSON 格式的字符串
$json = '{"a":"php","b":"mysql","c":3}';
其中a為鍵,php為a的鍵值。
實例:
<?php
$json = '{"a":"php","b":"mysql","c":3}';
$json_Class=json_decode($json);
$json_Array=json_decode($json, true);
print_r($json_Class);
print_r($json_Array);
?>
程序輸出:
stdClass Object (
[a] => php
[b] => mysql
[c] => 3 )
Array (
[a] => php
[b] => mysql
[c] => 3 )
在上面代碼的前提下訪問對象類型$json_Class的a的值
echo $json_Class->{'a'};
程序輸出:php
訪問數(shù)組類型$json_Array的a的值
echo $json_Array['a'];
程序輸出:php
相關(guān)教程:
PHP微課——理論實戰(zhàn)兩不誤
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: