位置: 编程技术 - 正文

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

发布时间:2024-01-03
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函数

  • 信息化投入包括手机吗
  • 金税四期税务机关一局式
  • 高新企业收到政府补贴怎么做账
  • 发票勾选错了已报税怎么办
  • 医院财政资金免企业所得税吗
  • 商业折扣和销售折让计入财务费用吗
  • 金税盘服务费能抵税吗
  • 子公司注销后账务如何处理
  • 人员工资成本占收入多少合适,占总成本多少合适
  • 客运承运人是什么意思
  • 材料存货的期末计量有何特点
  • 增值税电子发票怎么作废
  • 兼职取酬案例剖析
  • 合同是怎么影响企业纳税的?
  • 出售房产收入计入什么科目
  • 理财赎回本金没赎回利息咋办
  • 所得税汇算清缴分录怎么做
  • 小规模附加税申报表填写范本
  • 向境外企业支付服务费代扣代缴什么税
  • 本月没有发生额,月末怎么结账
  • 可转换债券具有债权和股权双重特征
  • 税法中的视同行为如何开具发票
  • 技术服务费增值税发票怎么开
  • bios设置内存频率后黑屏
  • php正则表达式实例
  • 职工教育经费怎么记账
  • 税后利润是股东权益吗
  • rtlrack.exe - rtlrack是什么进程 有什么用
  • 未取得发票入账违反什么规定
  • PHP:imagesetstyle()的用法_GD库图像处理函数
  • 其他业务成本的意思
  • 企业备用金管理制度规定
  • 其他收益年末需要结转吗
  • 缅甸安达曼海
  • php中strstr
  • 酒类销售企业广告宣传语
  • 小企业会计准则和一般企业会计准则的区别
  • vue 动态添加路由
  • code editing
  • redis zset源码
  • php array合并
  • 低值易耗品费用部门
  • 小规模固定资产处置税率
  • 残疾基金出什么科目
  • 损益类所得税费用
  • nosql manager for mongodb
  • Linux sqlite3 基本命令
  • 开票信息修改
  • 购买设备配件
  • 影响留存收益变动的选折题
  • 普通发票一般几个点
  • 白条入账会计处理
  • 购买的服务如何申请退款
  • 2021劳动报酬收入个人所得税怎么算
  • 待处理财产损益的账务处理
  • 小规模纳税人减按1%政策
  • 职工薪酬怎么核算
  • 民办非企业单位有哪些
  • 进项税额转出忘记结转
  • 制造费用分配结转表
  • mysql写错了怎么结束
  • win7传真和扫描
  • win8系统开机界面
  • bios界面如何进入
  • p2p是什么文件
  • win7系统打开软件慢的原因
  • mac怎么设置屏幕显示时间
  • Windows Server 2012实用技巧集锦
  • ext.grid.gridpanel
  • jquery中获取元素里边内容用什么方法
  • shell脚本-lt
  • css中dl
  • windows 2002关机
  • python进行aes解密
  • unity ugui合批
  • jquery控制display属性
  • 跑马灯 css
  • ca证书怎么下载安装
  • 江苏税务电子税务局电话号码
  • 江苏房产税如何计算公式
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号