全部
常见问题
产品动态
精选推荐

PHP函数json_decode的用法,PHP json_decode()用法及代码示例

管理 管理 编辑 删除

json_decode()函数是PHP中的内置函数,用于解码JSON字符串。它将JSON编码的字符串转换为PHP变量。

用法:

json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 )

参数:该函数接受上述和以下所述的四个参数:

json:它包含需要解码的JSON字符串。它仅适用于UTF-8编码的字符串。

assoc:它是一个布尔变量。如果为true,则返回的对象将转换为关联数组。

depth:它规定了用户指定的递归深度。

options:它包括JSON_OBJECT_AS_ARRAY,JSON_BIGINT_AS_STRING,JSON_THROW_ON_ERROR的位掩码。

返回值:此函数以适当的PHP类型返回编码的JSON值。如果无法解码json或编码数据比递归限制深,则它将返回NULL。

以下示例说明了PHP中json_decode()函数的用法:

示例1:

// Declare a json string
$json = '{"g":7, "e":5, "e":5, "k":11, "s":19}';
// Use json_decode() function to
// decode a string
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>

输出:

object(stdClass)#1 (4) {
["g"]=>
int(7)
["e"]=>
int(5)
["k"]=>
int(11)
["s"]=>
int(19)
}
array(4) {
["g"]=>
int(7)
["e"]=>
int(5)
["k"]=>
int(11)
["s"]=>
int(19)
}

示例2:

// Declare a json string
$json = '{"geeks": 7551119}';
// Use json_decode() function to
// decode a string
$obj = json_decode($json);
// Display the value of json object
print $obj->{'geeks'};
?>

输出:

7551119

使用json_decode()函数时的常见错误:

使用的字符串是有效的JavaScript,但不是有效的JSON。

名称和值必须用双引号引起来,不允许使用单引号。

不允许使用逗号结尾。

CRMEB-慕白寒窗雪 最后编辑于2023-05-29 17:52:44

快捷回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
回复从新到旧

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest==1? '取消推荐': '推荐'}}
{{item.floor}}#
{{item.user_info.title}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

作者 管理员 企业

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}   {{itemc.ip_address}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
回复
回复
1233
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

CRMEB-慕白寒窗雪 作者
社区运营专员---高冷のBoy | 呆萌のGirl

回答

2096

发布

1773

经验

44510

快速安全登录

使用微信扫码登录
{{item.label}} {{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
加精
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 开源下载 CRMEB官方论坛 帮助文档
返回顶部 返回顶部
CRMEB客服