位置: 编程技术 - 正文

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函数

  • 初装费包括什么
  • 计提利息要计提手续费吗
  • 增值税应纳税额的计算
  • 契税是什么意思契税是过户费吗
  • 领用库存商品用于固定资产
  • 太阳能发电税收优惠政策
  • 定期定额自行申报流程
  • 一般纳税人资格认定条件
  • 外单位人员报销差旅费会计分录
  • 金税三期申报代扣代缴增值税流程步骤分析
  • 银行利息收入确认时间会计与税法的差异
  • 公司做账是做什么
  • 一般纳税人的工资可以抵扣吗
  • 免税发票还可以开吗
  • 钢结构安装有哪些工种
  • 个人取得利息需要缴纳增值税吗
  • 本期增加固定资产原值
  • 转口贸易是否缴增值税
  • 种子销售公司
  • 分期付款购买商品
  • 应税劳务、服务清单
  • 4月发票能入1月吗
  • 营改增后哪些费用可以抵扣
  • 如果被客户骗了货款怎么办
  • 其他资金结转结余包括哪些
  • 公司之间往来借款利息怎么记账
  • 简易征收办法征收一般纳税人
  • 大白菜u盘启动后黑屏
  • 实缴资金少有什么风险
  • 购买加油卡如何开发票
  • 发票中的密码区是如何形成的
  • php照片上传
  • php登录注册整套源码
  • 生日快乐html5代码
  • ls -lh命令
  • 服装店快递费
  • phpcms上传视频
  • 旅行社代订的机票可以退吗
  • day15-Servlet04
  • 织梦如何给栏目增加缩略图
  • 小微企业城建税减免政策
  • 退货开红字发票,已经交了消费税的怎么处理
  • 新办企业装修会计分录
  • sql编程软件哪个好用
  • 实收资本印花税是一年一交吗
  • 应付股利科目怎么结转
  • 银行发放执行款多久到账
  • 企业网银代发工资明细打印
  • 坏账准备的核算公式
  • 应收票据的计价应按
  • 企业搬迁安置费一般怎么赔
  • 包工包料怎么开13个点发票
  • 预交了增值税款可以退吗
  • 社保个人部分应该做到哪个科目
  • 汇算清缴退款如何做账
  • 套现给现金还是转账好
  • 先出报表还是先报表
  • 财务费用包括哪几项
  • win2000注册表
  • Windows Server 2003服务器安全设置
  • linux系统 虚拟机
  • freebsd 升级
  • win8无法正常启动你的电脑
  • linux 图形
  • ubuntu 无法正常启动
  • WinXP老显示器CRT显示器严重闪屏的修复方法
  • linux安装fping
  • win10怎么安装google浏览器
  • win7 64位旗舰版下载后怎样禁用搜索记录?win7禁用搜索记录的设置方法
  • win7 32位旗舰版电脑城下载
  • Bullet(Cocos2dx)之增加调试绘制PhysicsDraw3D
  • eval()方法
  • jquery日期控件onchange事件
  • img可以设置的属性
  • python开发技术详解(全27集),5
  • unity3d添加组件
  • 换发票需要交钱吗
  • 单位自有住房免增值税吗
  • 开票日期是今年但是业务是去年
  • 成品油进货
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设