位置: 编程技术 - 正文

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

  • 已申报税额是什么意思,要交吗
  • 增值税退税如何做账
  • 开具电费发票如何入账?
  • 以前年度无形资产本年摊销额
  • 纳税申报表保留几年
  • 应付利润借方有余额怎么处理
  • 购车保险返现如何做会计凭证
  • 驾驶员行车补贴开什么发票
  • 全年一次性奖金税率表
  • 销售货物分期付款怎么确认收入
  • 预缴税款是否可以强制执行
  • 企业发票专用章使用涉及的审批流程
  • 置换上市公司资质要求
  • 高新技术企业外债便利化
  • 制造费用工资结转怎么算
  • 母公司收购少数股东股权,之前的利润母公司不享有
  • 企业购入旧固定资产如何计提折旧
  • 私人帐户可以给别人用吗
  • 对外报价时应考虑哪些因素
  • 劳务发票可以做费用吗怎么做
  • 技术研发费加计扣除政策
  • 进项税和销项税税率一样吗
  • 外购无形资产的成本包括进口关税吗
  • 税控盘服务费申报是在哪里填写
  • 小企业流动资产一般是多少
  • 资金筹集业务的会计处理
  • 民间非营利组织会计制度会计科目
  • 房地产企业收取的诚意金
  • 工会资助贫困大学生
  • 工资和薪金是一回事吗
  • 未分配利润亏损怎么做分录
  • won10字体变大
  • bash是什么命令
  • rundll32进程很多
  • 修改注册表限制cpu速度
  • 应付债券借贷
  • 未到期的应收票据向银行贴现什么时候计入短期贷款
  • vue 首页
  • 专利权出资会计科目
  • 资产负债表根据明细账余额填列的项目
  • 材料短缺计入
  • 往境外支付培训费怎么退
  • PHP+MySql+jQuery实现的"顶"和"踩"投票功能
  • 实用的开源软件
  • 前端跨域解决方案设计
  • 十四届智能车规则
  • 融资租入固定资产的账务处理
  • python condition条件变量
  • mysql基本命令大全
  • 呆账核销账务处理程序
  • sqlserver 数据迁移
  • 固定资产折旧的影响因素
  • 政府对企业提交的项目申请报告主要从等方面进行核准
  • 股东分红会计分录
  • 计提工会经费是什么凭证
  • 金蝶财务软件固定资产
  • 单位定期存单利息如何记账
  • 委托加工业务的财务职责
  • 收到预付卡的发票是否可以报销呢?
  • 微信提现手续费多少?
  • 临时工工资如何做账
  • 公司支付账户
  • 新会计准则会计科目明细表
  • 每月分红会计分录
  • 私企银行有哪几家
  • mysql中排序规则是什么意思
  • mysql5.7.35安装配置教程
  • ftp的安装
  • java调用so库文件
  • 联想笔记本bios设置启动顺序
  • windows 安装源
  • win8系统怎么创建局域网
  • window10安装时间太长
  • WIN10安装网络打印机
  • 置顶pyq会被自己屏蔽的人看到吗
  • 查找第一个字符
  • javascript中的对象用于
  • 全面解析白羊座o型血女
  • 国家税务局文件查询
  • 地方税务局办税流程
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设