❶ 用php如何將數據(文本和圖片)轉換成json格式
json和php數組 格式的互相轉換,參考如下:
$php_json = json_encode($json_arr); //把php數組格式轉換成 json 格式的數據
echo $php_json;
$php_json = json_decode($php_json); //再把json格式的數據轉換成php數組
print_r($php_json);
json數組
$json = '[{"id":"22","name":"33","descn":"44"}]'; //json格式的數組轉換成 php的數組
$arr = (Array)json_decode($json);
echo $arr[0]->id; //用對象的方式訪問。
❷ php傳過來的json數據js怎麼調用
json對象中的屬性用 「.」(點)來指向訪問。
如:
jsonData是返回的json數據。
jsonData.username jsonData.id
這樣調用
❸ php 怎麼 字元串 轉 json對象
需要准備的材料分別是:電腦、php編輯器、瀏覽器。
1、首先,打開php編輯器,新建php文件,例如:index.php,裡面定義了個一個json字元串。
❹ PHP如何循環json
需要先將json轉換成數組,然後才能循環。
json是字元串,不能直接循環。使用json_decode($jsonstring, true) 可以將格式正確的json字元串轉換成關聯數組。
需要注意,該函數只能處理UTF-8編碼的json字元。
實例代碼:
<?php
$json='{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_mp(json_decode($json));
var_mp(json_decode($json,true));
?>
以上實例將會輸出:
object(stdClass)#1(5){
["a"]=>int(1)
["b"]=>int(2)
["c"]=>int(3)
["d"]=>int(4)
["e"]=>int(5)
}
array(5){
["a"]=>int(1)
["b"]=>int(2)
["c"]=>int(3)
["d"]=>int(4)
["e"]=>int(5)
}
?>