位置: 编程技术 - 正文

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

  • 纳税义务发生时间 纳税期限
  • 税控盘是干什么用的
  • 股东退股如何清算表格
  • 固定资产一次性计入费用的账务处理
  • 收到境外汇款怎么处理
  • 开了红字信息表当月怎么做账
  • 自产自销农产品企业所得税
  • 税务局开运费发票需要带什么
  • 可转换公司债券属于哪一类金融资产
  • 会计中借方和贷方怎么理解
  • 固定资产领用流程图
  • 应交所得税的计算例题
  • 账本印花税怎么缴纳
  • 小规模纳税人还是一般纳税人
  • 商品折扣与折让属于销售费用吗
  • 支出凭证的日期
  • 汇算清缴退税调整利润表
  • 合伙人退伙资产清算需要交税吗
  • 所得税前允许列支的税金包括
  • 库存商品无发票所得税怎么调增
  • mac 装 windows
  • 劳务报酬个税计算公式表
  • uefi启动u盘安装win10系统的详细流程
  • 一次性收取一年租金
  • 路由器重启后电视放不出来咋办
  • launcher.exe什么意思
  • 贸易型企业能开什么增值税票
  • PHP:gettimeofday()的用法_Date Time函数
  • 土地增值税清算管理规程
  • 存货损失是指什么
  • 固定资产是指什么
  • 工会经费是按应发工资还是实发工资申报
  • 民营医院所得税税率
  • php文件上传后只显示了路径怎么办啊
  • 使用灭火器人要站在上风口还是下风口
  • php的输出语句主要有哪些
  • 长期待摊费用科目怎么使用
  • 什么是企业的应付账款
  • 过路费属于会计哪个科目
  • vue实现注册
  • 织梦前台的菜单怎么换
  • 什么科目需要结转到本年利润
  • 税率的确定和什么因素有关
  • 以前年度未入账固定资产账务处理
  • 合伙企业需要做工商吗
  • 公司裁员怎么补偿
  • 固定资产为什么要交税
  • 高新技术企业补助需要交所得税吗
  • 营改增行业销售额的确定
  • 以前年度损益调整是什么意思
  • 存货类明细分类账一般采用
  • 出售无形资产损失
  • 旅客运送的一般规定
  • 小规模纳税人申请一般纳税人流程
  • 营改增后建筑企业税负不降反增
  • 增值税购进扣税法
  • 股息红利等权益性投资收益
  • 收付实现制和权责发生制区别
  • 长期待摊费用怎么做账
  • 小企业持有的不良资产
  • mysql 1449解决
  • sql如何实现循环
  • win7怎么彻底删除文件
  • 苹果mac系统复制粘贴不了
  • java中初始化顺序
  • 安卓专业图片处理软件
  • Python常用时间操作总结【取得当前时间、时间函数、应用等】
  • unity3d 物体移动
  • 关于全局变量和局部变量说法不正确的是
  • JavaScript中getUTCMinutes()方法的使用详解
  • python设计程序
  • 四川省税务干部学校官网
  • 红字专用发票进什么科目
  • 回迁房子契税怎么算
  • 比亚迪车载物联网卡
  • 个人所得税的网址
  • 浙江国税网上报税
  • 联通怎么在网上营业厅更改套餐
  • 签订设计合同
  • 定额发票怎么查流向
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设