php解析Json,json_encode()函數用法

接受一個 JSON 格式的字符串并且把它轉換為 PHP 變量

定義和用法


json_decode(json_str,true) 把JSON 格式的字符串轉換為 PHP數組或對象

語法:

json_decode(value,option) 

value:必填。待解碼的json字符串 。該函數只能接受 UTF-8 編碼的數據

option:可選 默認 false 轉為對象。true 轉為數組。

例子:


<?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)
}


原文鏈接:php解析Json,json_decode()函數用法