位置: 编程技术 - 正文

PHP:json_decode()的用法_JSON函数(php json)

编辑:rootadmin
json_decode

推荐整理分享PHP:json_decode()的用法_JSON函数(php json),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:php json,php json_pretty_print,php的json_encode,php jsondecode,php json_encode中文,php的json_encode,php的json_encode,php的json_encode,内容如对您有帮助,希望把文章链接给更多的朋友!

(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

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

说明 mixed json_decode ( string $json [, bool $assoc = false [, int $depth = [, int $options = 0 ]]] )

接受一个 JSON 格式的字符串并且把它转换为 PHP 变量

参数

json

待解码的 json string 格式的字符串。

This function only works with UTF-8 encoded data.

assoc

当该参数为 TRUE 时,将返回 array 而非 object 。

depth

User specified recursion depth.

options

Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)

返回值

Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

范例

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));?> PHP:json_decode()的用法_JSON函数(php json)

以上例程会输出:

Example #2 Accessing invalid object properties

Accessing elements within an object that contain characters not permitted under PHP&#;s naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

<?php$json='{"foo-bar":}';$obj=json_decode($json);print$obj->{'foo-bar'};//?>

Example #3 common mistakes using json_decode()

<?php//thefollowingstringsarevalidJavaScriptbutnotvalidJSON//thenameandvaluemustbeenclosedindoublequotes//singlequotesarenotvalid$bad_json="{'bar':'baz'}";json_decode($bad_json);//null//thenamemustbeenclosedindoublequotes$bad_json='{bar:"baz"}';json_decode($bad_json);//null//trailingcommasarenotallowed$bad_json='{bar:"baz",}';json_decode($bad_json);//null?>

Example #4 depth errors

<?php//Encodethedata.$json=json_encode(array(1=>array('English'=>array('One','January'),'French'=>array('Une','Janvier'))));//Definetheerrors.$constants=get_defined_constants(true);$json_errors=array();foreach($constants["json"]as$name=>$value){if(!strncmp($name,"JSON_ERROR_",)){$json_errors[$value]=$name;}}//Showtheerrorsfordifferentdepths.foreach(range(4,3,-1)as$depth){var_dump(json_decode($json,true,$depth));echo'Lasterror:',$json_errors[json_last_error()],PHP_EOL,PHP_EOL;}?>

以上例程会输出:

Example #5 json_decode() of large integers

<?php$json='';var_dump(json_decode($json));var_dump(json_decode($json,false,,JSON_BIGINT_AS_STRING));?>

以上例程会输出:

注释

Note:

The JSON spec is not JavaScript, but a subset of JavaScript.

Note:

In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error.

更新日志

版本 说明 5.4.0 The options parameter was added. 5.3.0 Added the optional depth. The default recursion depth was increased from to 5.2.3 The nesting limit was increased from to 5.2.1 Added support for JSON decoding of basic types.

参见

json_encode() - 对变量进行 JSON 编码 json_last_error() - 返回最后发生的错误

PHP:json_last_error()的用法_JSON函数 json_last_error(PHP5=5.3.0)json_last_error返回最后发生的错误说明intjson_last_error(void)如果有,返回JSON编码解码时最后发生的错误。参数此函数没有参数。返回值

PHP:json_last_error_msg()的用法_JSON函数 json_last_error_msg(PHP5=5.5.0)json_last_error_msgReturnstheerrorstringofthelastjson_encode()orjson_decode()call说明stringjson_last_error_msg(void)参数此函数没有参数。返回值Returnstheer

PHP:system()的用法_命令行函数 system(PHP4,PHP5)system执行外部程序,并且显示输出说明stringsystem(string$command[,int&$return_var])同C版本的system()函数一样,本函数执行command参数所指定的命令,

标签: php json

本文链接地址:https://www.jiuchutong.com/biancheng/284697.html 转载请保留说明!

上一篇:PHP:json_encode()的用法_JSON函数(php jsondecode)

下一篇:PHP:json_last_error()的用法_JSON函数

  • 固定资产更新改造资本化条件
  • 银行转账支付计入什么科目
  • 稽查查补的税款用什么时候的税率表示
  • 收到商业承兑汇票到期,向银行办妥有关入账手续
  • 要约收购期满
  • 员工报销没有发票怎么入账
  • 企业的财务报表
  • 房租费没有发票怎么做账务处理
  • 仓库常用包材
  • 工程预付款有发票怎么做凭证
  • 自产产品用于销售费用
  • 企业将存货低价出售处理要如何做会计处理?
  • 主营业务税金及附加包括什么
  • 外贸出口企业城市排名
  • 公司去大厅交社保要带什么证件
  • 股票期权个人所得税税率表
  • 应纳税所得额应纳税额公式
  • 国外消费发票可以抵扣吗
  • 1697510302
  • 总分类账是什么的依据
  • php中运算符
  • 冲减预付款
  • 商业会计结转成本
  • 计提存货减值准备符合可靠性原则
  • 无形资产的确认与计量
  • bboy.exe进程是病毒吗 bboy进程安全吗
  • 纳税人解除劳动合同补偿
  • 计提固定资产折旧怎么做会计科目
  • 酒店没有营业执照可以举报吗?
  • wordpress使用
  • vue2动态路由
  • 皮丘拉湖畔的乌代布尔城市宫殿,印度 (© Chaiyun Damkaew/Getty Images)
  • 纳税人性质是什么
  • php实现支付宝支付
  • 企业年度汇算清缴申报表填写
  • 人防车库成本能抵扣吗
  • 业务预算包括直销费用吗
  • MySQL常用命令关键字
  • 以件数为印花税计税的有哪些
  • 残保金会计分录人员经费
  • 代缴社保费怎么做分录
  • 财务费用报销流程图word
  • 两个公司之间怎么走账
  • 收到的出口退税款需要并入利润总额吗
  • 采购方退货的会计分录
  • 主营业务成本可以直接贷银行存款吗
  • 递延所得税转回税率不一致
  • 增加个税信息怎么增加
  • 残保金在网上怎么申报
  • 建账的要点
  • 商业企业会计制度变迁论文
  • Ubuntu 12.04/14.04 LTS版内核更新 修复七个重大安全漏洞
  • freebsd ntfs
  • vmware 启动虚拟机
  • win8系统进不了桌面怎么办
  • xp开始界面
  • 使用linux常用命令
  • 戴尔dell服务器维修
  • ntfs硬盘分区整数
  • win10开机自动弹出设置界面
  • 电脑windows8怎么样
  • mac火狐浏览器flash插件设置
  • 更新win8
  • linux的内存
  • win8安装步骤
  • win7网速很慢
  • 你会支持国产系统吗英文
  • linux tcp keepalive
  • javascript entries
  • javascript函数大全
  • 什么叫懒加载
  • html通配符选择器怎么用
  • python变量字符串等函数
  • shell脚本输出空格
  • js教程
  • python字典键值对个数
  • 河南省发票查询真伪
  • 拟录用人员公示后还会递补吗
  • 车辆缴纳购置税多少钱
  • 划拨土地评估价
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设