php中json_decode返回数组或对象
2016-07-25 11:26
573 查看
转自:http://www.3lian.com/edu/2014/02-11/128395.html
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() 的例子
可以看出经过json_decode()编译出来的是对象,现在输出json_decode($data,true)试下
可以看出 json_decode($data,true)输出的一个关联数组,由此可知json_decode($data)输出的是对象,而json_decode("$arr",true)是把它强制生成PHP关联数组.
假如我们获取的JSON数据如下:(可以使用curl、fsockopen等方式获取)
一、json_decode返回array的方式:
json_decode($data,true);用json_decode函数返回array的方式得到:
我们在PHP语言中可以用以下方法取得我们想要的值:
二、json_decode返回object的方式:
json_decode($data);
用json_decode函数返回object的方式得到:
我们在PHP语言中可以用以下方法取得我们想要的值:
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) } $data='[{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""}]'; echo json_decode($data); 结果为: Array ( [0] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) ) |
代码如下 | |
echo json_decode($data,true); 结果: Array ( [0] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) ) |
假如我们获取的JSON数据如下:(可以使用curl、fsockopen等方式获取)
代码如下 | |
{ "from":"zh", "to":"en", "trans_result":[ { "src":"u4f60u597d", "dst":"Hello" } ] } |
json_decode($data,true);用json_decode函数返回array的方式得到:
代码如下 | |
Array ( [from] => zh [to] => en [trans_result] => Array ( [0] => Array ( [src] => 你好 [dst] => Hello ) ) ) |
代码如下 | |
<?php $data = <<<STR { "from":"zh", "to":"en", "trans_result":[ { "src":"u4f60u597d", "dst":"Hello" } ] } STR; $jsondata=json_decode($data,true); header("Content-Type: text/html; charset=UTF-8"); print_r($jsondata);www.111cn.net echo "<br />".$jsondata['to']; //en echo "<br />".$jsondata['trans_result'][0]['dst']; //Hello ?> |
json_decode($data);
用json_decode函数返回object的方式得到:
代码如下 | |
stdClass Object ( [from] => zh [to] => en [trans_result] => Array ( [0] => stdClass Object ( [src] => 你好 [dst] => Hello ) ) ) |
代码如下 | |
<?php $data = <<<STR { "from":"zh", "to":"en", "trans_result":[ { "src":"u4f60u597d", "dst":"Hello" } ] } STR; $jsondata=json_decode($data); header("Content-Type: text/html; charset=UTF-8"); print_r($jsondata); echo "<br />".$jsondata->from; //zh echo "<br />".$jsondata->trans_result[0]->src; //你好 ?> |
相关文章推荐
- php下载代码
- 嵌入式开发12天(虚拟机的环境配置,目录的快捷方式,交叉编译工具的安装,tftp的安装,nfs的安装)
- PHP截取中文无乱码函数——cutstr
- PHP中$_SERVER的详细参数与说明
- PHP 中转换 SGML 类语言为真实中文字符
- NoSuchMethodErroe:AnnotationUtils.postProcessAnnotationAttributes
- phpBB论坛 代码 语法高亮 模块 Codebox Plus
- Thinkphp url重写规则及wamp配置
- PHP基本知识收集
- Yii2中Restful API原理实例分析
- php处理Excel步骤介绍
- PHP 5.4 的 Trait 特性_实现类的多重继承
- FastCgi与PHP-fpm之间是个什么样的关系
- 教你如何搭建自己的FTP服务器
- PHP的线程安全与非线程安全版本的区别
- PHP生成唯一会员卡号
- 详解PHP实现定时任务的五种方法
- 15 个实用的 PHP 正则表达式
- Yii2中YiiBase自动加载类、引用文件方法分析(autoload)
- 【ThinkPHP】中,对配置项做了个详细的总结~