博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php中json_decode返回数组或对象的实例
阅读量:4308 次
发布时间:2019-06-06

本文共 2929 字,大约阅读时间需要 9 分钟。

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] => ) )

 

可以看出经过json_decode()编译出来的是对象,现在输出json_decode($data,true)试下

 代码如下 复制代码

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_decode($data,true)输出的一个关联数组,由此可知json_decode($data)输出的是对象,而json_decode("$arr",true)是把它强制生成PHP关联数组.

假如我们获取的JSON数据如下:(可以使用curl、fsockopen等方式获取)

 代码如下 复制代码

{
 "from":"zh",
 "to":"en",
 "trans_result":[
  {
   "src":"u4f60u597d",
   "dst":"Hello"
  }
 ]
}
 

一、json_decode返回array的方式:

json_decode($data,true);用json_decode函数返回array的方式得到:

 代码如下 复制代码

Array
(
    [from] => zh
    [to] => en
    [trans_result] => Array
        (
            [0] => Array
                (
                    [src] => 你好
                    [dst] => Hello
                )

        )

)

 

我们在PHP语言中可以用以下方法取得我们想要的值:

 代码如下 复制代码

<?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返回object的方式:

json_decode($data);

用json_decode函数返回object的方式得到:

 代码如下 复制代码

stdClass Object
(
    [from] => zh
    [to] => en
    [trans_result] => Array
        (
            [0] => stdClass Object
                (
                    [src] => 你好
                    [dst] => Hello
                )

        )

)

 

我们在PHP语言中可以用以下方法取得我们想要的值:

 代码如下 复制代码

<?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; //你好
?>
 

更多详细内容请查看:

转载于:https://www.cnblogs.com/alibai/p/3547485.html

你可能感兴趣的文章
jQuery 学习笔记(jQuery: The Return Flight)
查看>>
Java中常用的测试工具JUnit
查看>>
PHP图形图像的典型应用 --常用图像的应用(验证码)
查看>>
Robot Framework-Ride界面介绍及库的添加
查看>>
IntelliJ IDEA 连接数据库 详细过程
查看>>
redis完全攻略
查看>>
D3---01基础的柱状图制作(转)
查看>>
Time-Varying Mesh Compression
查看>>
SocketServer源码学习(二)
查看>>
编写DLL所学所思(1)——导出函数
查看>>
POJ3006-Dirichlet's Theorem on Arithmetic Progressions
查看>>
QT分页控件,开源,供大家使用
查看>>
005.LVM删除
查看>>
Hibernate 简介(百度)
查看>>
深入理解 KVC\KVO 实现机制 — KVC
查看>>
Android develop 国际化
查看>>
快速求幂算法
查看>>
Freemarker模板引擎
查看>>
jQuery:表格的奇偶行变色,jquery实例之表格隔一行
查看>>
(Object-C)学习笔记(一)--开发环境配置和与c语言的区别
查看>>