【PHP】php中json_decode()和json_encode()
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)
}
2.json_encode()
json_encode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_encode — 对变量进行 JSON 编码
Report a bug 说明
string json_encode ( mixed $value [, int $options = 0 ] )
返回 value 值的 JSON 形式
Report a bug 参数
value
待编码的 value ,除了resource 类型之外,可以为任何数据类型
该函数只能接受 UTF-8 编码的数据
options
由以下常量组成的二进制掩码: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE.
Report a bug 返回值
编码成功则返回一个以 JSON 形式表示的 string 或者在失败时返回 FALSE 。
Report a bug 更新日志
版本 说明
5.4.0 options 参数增加常量: JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, 和 JSON_UNESCAPED_UNICODE。
5.3.3 options 参数增加常量:JSON_NUMERIC_CHECK。
5.3.0 增加 options 参数.
Report a bug 范例
Example #1 A json_encode() 的例子
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
然后输出
{"a":1,"b":2,"c":3,"d":4,"e":5}
【PHP】php中json_decode()和json_encode()的更多相关文章
- php中json_decode()和json_encode()的使用方法
php中json_decode()和json_encode()的使用方法 作者: 字体:[增加 减小] 类型:转载 json_decode对JSON格式的字符串进行编码而json_encode对变 ...
- php 中json_decode()和json_encode()的使用方法
1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...
- php中json_decode()和json_encode()
1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...
- json_decode()和json_encode()的使用方法
json_decode对JSON格式的字符串进行编码 json_encode对变量进行 JSON 编码 JS中对JSON的解析 一.JSON字符串转换为JSON对象 要运用上面的str1,必须 ...
- json_decode()和json_encode()区别----2015-0929
json_decode对JSON格式的字符串进行编码而json_encode对变量进行 JSON 编码,需要的朋友可以参考下 1.json_decode() json_decode (PHP 5 ...
- php中json_decode返回数组或对象
http://www.3lian.com/edu/2014/02-11/128395.html 1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL ...
- 解决php中json_decode的异常JSON_ERROR_CTRL_CHAR
该字符中含了ASCII码ETB控制符,即\x17导致json解析失败 (截图中显示ETB是因为用了Sublime text2) 解决方法如下:去掉0-31的控制符再进行decode $result = ...
- json_decode 与 json_encode 的区别
1.json_decode对JSON格式的字符串进行编码 2.json_encode对变量进行 JSON 编码 3.unset()是注销定义的变量 4.urlencode()函数原理就是首先把中文字符 ...
- php中json_decode返回数组或对象的实例
1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...
随机推荐
- HDU 5734 A - Acperience
http://acm.hdu.edu.cn/showproblem.php?pid=5734 Problem Description Deep neural networks (DNN) have s ...
- java里如何实现两个等长度的字符串数组有多少个元素相同(从最左边开始,一旦遇到不同元素则跳出计数)
不多说,直接上干货! package zhouls.bigdata.DataFeatureSelection.sim; public class test { public static int st ...
- IO缓冲流
目录 IO缓冲流 缓冲流 基本原理 字节缓冲流 字符缓冲流 IO缓冲流 缓冲流也叫高效流,能够更高效的进行读取: 转换流:能够进行编码转换 序列化流:持久化存储对象 缓冲流 缓冲流--就是对应4个Fi ...
- FFmpeg编译i386 OSX 脚本
话不多说, 直接上脚本 #!/bin/sh # directories PLATFORM="OSX" # FFmpeg脚本目录 SOURCE="ffmpeg-2.8.7& ...
- CentOS6.5 环境安装配置
一.GO环境配置 1.运行命令进入/usr/local/src目录:cd /usr/local/src 2.下载安装包:运行wget --no-check-certificate https://st ...
- Day6 盒模型
Day6 盒模型 1.一.标准盒模型(w3c盒模型) 1)组成部分: content + padding + border + margin 内容 ...
- cron 任务执行表达式
1.来源 开始我还不知道cron到底来源于哪里,不求甚解的我也没做过多了解,现在突然用到所以写一下. cron计划任务 其实只是linux 一个执行计划的一个工具或者执行程序. 在Linux系统中, ...
- 私有npm下载资源
私有npm库下载资源需要用户名和密码,这个需要创建npm库的人提供. 使用方法: npm login --registry=仓库地址 Username: 用户名 Password: 密码 Email: ...
- Java设计模式—装饰模式
装饰模式是一种比较常见的模式. 定义为:动态的给一个对象添加一些额外的职责.就增加功能来说,装饰模式比生成子类更加灵活. 装饰模式的通用类图如下: 装饰模式的构成: 1) 抽象构件(Component ...
- CPU保护模式DPL、CPL简易理解
现代INTEL CPU都有保护模式,实模式这两种CPU运行模式.当CPU加电,CPU初始化时就运行在是模式下,然后现代操作系统会从实模式跳转到保护模式! 为什么需要保护模式? 在最开始编程的汇编时代, ...