位置: 编程技术 - 正文

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

  • 转让金融商品需要缴纳增值税吗
  • 房地产零税率
  • 无票收入在增值税表中填哪
  • 公司购买新车购置税是否可以抵扣呢
  • 报销金额大于发票金额几毛钱
  • 法人工资怎么发最划算
  • 未确认融资费用和长期应付款
  • 关联企业房产转让
  • 资产负债表应交税金负数是什么意思
  • 公司购买的电脑怎么做账
  • 技术证怎么办理
  • 政策性搬迁损失赔偿标准
  • 溢价发行债券的发行价格怎么计算的
  • 银行开户时存入的现金怎么做账
  • 股票回购后多久注销
  • 其他收益是否缴纳个税
  • 国税和地税归哪里管
  • 水利基金的计税依据是含税收入还是不含税收入
  • 水利建设专项收入怎么计算
  • 出纳记账本如何手工记账
  • 记账公司如何平衡收入成本费用?
  • 财务杠杆系数简单计算方法
  • 交易性金融资产属于流动资产吗
  • 医保退休必须交多少年辽阳
  • 移动通信定额发票
  • 建筑服务预交税款怎么计算的
  • 经营租入的土地使用权 待摊
  • 收到分包公司工程发票收入怎么做账
  • 出售使用过的车辆如何交印花税呢
  • 特许权使用费属于销售收入吗
  • php的数组函数
  • 后端接口返回数据类型规范
  • vue实现导出
  • 猫鼬家族风云
  • php做网页
  • thinkphp3.1
  • vue鼠标点击事件点击改变效果,再次点击恢复效果
  • arc架构
  • 其他综合收益什么类科目
  • 图像边缘检测的基本原理
  • 医疗机构收据
  • 债务抵销的顺序
  • 幼儿园固定资产说明怎么写
  • 房地产公司收到客户违约金会计科目
  • 开发商出售商铺税费
  • 小规模季度超过45万了怎么缴纳
  • 没有收到房租发票
  • 企业职工食堂就餐收费
  • 增值税附加税减半政策
  • 营改增之后是不是就没有营业税了
  • 购进生产车间增值税税率
  • 小规模年末怎么做账
  • 施工企业会计成本费用论文3000字
  • 报销单会计分录怎么写
  • 多计费用以前年度损益调整账务处理
  • 库存不够如何结算成本
  • 给业务员的佣金怎么做账
  • 公司向个人借款利息可以税前扣除吗
  • 合作社盈余返还总额是指哪些数据
  • 个人经纪代理服务
  • win8.1安装失败
  • linux系统中用户密码保存在什么文件中
  • os x 10.11 el capitan中文版上手体验评测
  • ngwatch.exe
  • win7系统怎么禁用数字签名
  • 如何教新手
  • win7小技巧
  • win7 64位旗舰版下载后怎样禁用搜索记录?win7禁用搜索记录的设置方法
  • win10系统自带虚拟机怎么用
  • w10系统如何
  • jquery插件怎么写
  • android开发环境的搭建步骤
  • android studio unity3d
  • javascript语言介绍
  • python仿站软件官网
  • 会计餐费费的会计分录
  • 签订税务三方协议
  • 江苏税务局电子
  • 资源综合利用企业所得税优惠
  • 税务会计业务处理流程
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设