json_decode

PHP中json_decode怎么使用

小亿
106
2023-08-02 23:14:30
栏目: 编程语言

在PHP中,可以使用json_decode函数来将JSON格式的字符串解码为PHP对象或数组。

使用json_decode的基本语法如下:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

参数说明:

示例代码:

$jsonStr = '{"name":"John", "age":30, "city":"New York"}';
// 解码为对象
$obj = json_decode($jsonStr);
echo $obj->name;  // 输出 "John"
// 解码为关联数组
$arr = json_decode($jsonStr, true);
echo $arr['name'];  // 输出 "John"

注意事项:

0
看了该问题的人还看了