json如何轉(zhuǎn)php?
json_decode是php5.2.0之后新增的一個(gè)PHP內(nèi)置函數(shù),其作用是對(duì)JSON格式的字符串進(jìn)行編碼.
json_decode的語法規(guī)則:
json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
json_decode接受一個(gè)JSON格式的字符串并且把它轉(zhuǎn)換為PHP變量 ,當(dāng)該參數(shù)$assoc為TRUE時(shí),將返回array,否則返回object。
JSON 格式的字符串
$json = '{"a":"php","b":"mysql","c":3}';
其中a為鍵,php為a的鍵值。
實(shí)例:
<?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 )
在上面代碼的前提下
訪問對(duì)象類型$json_Class的a的值
echo $json_Class->{'a'};
程序輸出:php
訪問數(shù)組類型$json_Array的a的值
echo $json_Array['a'];
程序輸出:php