PHP中的json_encode和json_decode
生活随笔
收集整理的這篇文章主要介紹了
PHP中的json_encode和json_decode
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.json_decode()
?
json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
?
json_decode — 對 JSON 格式的字符串進行編碼
?
說明
mixed json_decode ( string $json [, bool $assoc ] )
接受一個 JSON 格式的字符串并且把它轉換為 PHP 變量
?
參數
?
json
待解碼的 json string 格式的字符串。
?
assoc
當該參數為 TRUE 時,將返回 array 而非 object 。
?
?
返回值
Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.
?
范例
?
Example #1 json_decode() 的例子
代碼如下:
<?php$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';var_dump(json_decode($json));var_dump(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)} 經過json_decode()編譯出來的是對象 ?json_decode($data,true)輸出的一個關聯數組, 2.json_encode() 只支持utf-8的編碼格式json_encode(PHP 5 >= 5.2.0, PECL json >= 1.2.0)json_encode — 對變量進行 JSON 編碼Report a bug 說明string json_encode ( mixed $value [, int $options = 0 ] )返回 value 值的 JSON 形式Report a bug 參數value待編碼的 value ,除了resource 類型之外,可以為任何數據類型總結
以上是生活随笔為你收集整理的PHP中的json_encode和json_decode的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js中setTimeout的用法和JS计
- 下一篇: 动态规划算法php,php算法学习之动态