PHP5.2.0及以上版本具有json_decode函数,该函数是用来解析JSON格式的数据,可以返回array(数组)或object(对象)两种结果,下面将分两种情况具体介绍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() 的例子
代码如下 | 复制代码 |
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; 上例将输出: object(stdClass)#1 (5) { array(5) {
结果为: 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等方式获取)
代码如下 | 复制代码 |
{ |
一、json_decode返回array的方式:
json_decode($data,true);用json_decode函数返回array的方式得到:
代码如下 | 复制代码 |
Array ) ) |
我们在PHP语言中可以用以下方法取得我们想要的值:
代码如下 | 复制代码 |
$data =
{ |
二、json_decode返回object的方式:
json_decode($data);
用json_decode函数返回object的方式得到:
代码如下 | 复制代码 |
stdClass Object ) ) |
我们在PHP语言中可以用以下方法取得我们想要的值:
代码如下 | 复制代码 |
$data =
{ STR; |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools
