json_decode — 对JSON 格式的字符串进行编码

2011-02-24 阅读 48

json_decode — 对JSON 格式的字符串进行编码

编辑本段函数定义
mixed json_decode ( string json [, bool assoc ] )
接受一个JSON 格式的字符串并且把它转换为 PHP 变量

参数
json
待解码的 json string 格式的字符串。
assoc
当该参数为 TRUE 时,将返回array而非 object 。

返回值
返回一个对象,如果assoc参数选项为true,将会返回一个关联数组。

编辑本段实例说明
用json_decode()函数将JSON 格式的字符串编码,以及使用第二个参数对生成变量的类型影响。

<pre class="brush:php;">    <?php
$arr = array ('L' => 'linux', 'A' => 'apache', 'M' => 'mysql', 'P' => 'php' );
$lamp = json_encode ( $arr );
echo '<pre />';
var_dump ( json_decode ( $lamp ) );
var_dump ( json_decode ( $lamp, true ) );
?>
```
<p>
	 以上例程会输出:<br><br>
	&nbsp;</p>
<pre class="brush:plain;">    object(stdClass)#1 (4) {
  ["L"]=>
  string(5) "linux"
  ["A"]=>
  string(6) "apache"
  ["M"]=>
  string(5) "mysql"
  ["P"]=>
  string(3) "php"
}

array(4) {
  ["L"]=>
  string(5) "linux"
  ["A"]=>
  string(6) "apache"
  ["M"]=>
  string(5) "mysql"
  ["P"]=>
  string(3) "php"
}
```

json_encode json_decode

更新于 2023年03月28日