欢迎访问宙启技术站
智能推送

10个使用JSON数据的PHP函数

发布时间:2023-06-10 10:50:38

JSON是一种轻量级的数据交换格式。它是基于JavaScript的语法,但可以被许多编程语言支持和解析,包括PHP。PHP内置了许多处理JSON数据的函数。以下是10个使用JSON数据的PHP函数:

1. json_encode()

这个函数将PHP数组转换为JSON格式的字符串。PHP数组可以包含字符串、数值、布尔值、对象、嵌套数组等数据类型。语法:

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

参数$value是要编码的PHP数据类型,$options是编码选项,$depth是递归深度。返回值是JSON格式的字符串。

示例:

$data = array(
    'name' => 'John',
    'age' => 30,
    'married' => true,
    'hobbies' => array('reading', 'traveling'),
    'address' => array('city' => 'New York', 'zip' => '10001')
);

$json = json_encode($data);

echo $json;

输出结果:

{"name":"John","age":30,"married":true,"hobbies":["reading","traveling"],"address":{"city":"New York","zip":"10001"}}

2. json_decode()

这个函数将JSON格式的字符串转换为PHP数组。语法:

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

参数$json是要解码的JSON字符串,$assoc是一个布尔值,如果设置为true,则将返回关联数组而非对象。$depth是递归深度,$options是解码选项。如果解码失败,则返回null。

示例:

$json = '{"name":"John","age":30,"married":true,"hobbies":["reading","traveling"],"address":{"city":"New York","zip":"10001"}}';

$data = json_decode($json, true);

print_r($data);

输出结果:

Array
(
    [name] => John
    [age] => 30
    [married] => 1
    [hobbies] => Array
        (
            [0] => reading
            [1] => traveling
        )

    [address] => Array
        (
            [city] => New York
            [zip] => 10001
        )

)

3. json_last_error()

这个函数返回最后一个JSON函数调用的错误代码。语法:

int json_last_error ( void )

如果没有错误发生,则返回JSON_ERROR_NONE。可以用于调试JSON解码或编码的错误。

示例:

$json = '{"name":"John","age":30,"married":true,"hobbies":["reading","traveling"],"address":{"city":"New York","zip":"10001"}}';

$data = json_decode($json, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'JSON解码失败:' . json_last_error_msg();
}
else {
    print_r($data);
}

4. json_last_error_msg()

这个函数返回最后一个JSON函数调用的错误信息。语法:

string json_last_error_msg ( void )

如果没有错误发生,则返回空字符串。可以用于调试JSON解码或编码的错误。

示例:

$json = '{"name":"John","age":30,"married":true,"hobbies":["reading","traveling"],"address":{"city":"New York","zip":"10001"}}';

$data = json_decode($json, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'JSON解码失败:' . json_last_error_msg();
}
else {
    print_r($data);
}

5. json_encode_options()

这个函数设置json_encode()函数的选项。语法:

void json_encode_options ( int $options )

可以设置的选项包括JSON_PRETTY_PRINT(格式化输出)、JSON_UNESCAPED_SLASHES(不转义斜杠)、JSON_UNESCAPED_UNICODE(不转义Unicode字符)等。

示例:

$data = array(
    'name' => 'John',
    'age' => 30,
    'married' => true,
    'hobbies' => array('reading', 'traveling'),
    'address' => array('city' => 'New York', 'zip' => '10001')
);

$json = json_encode($data);

// 默认选项
echo $json;

// JSON_PRETTY_PRINT选项
echo json_encode($data, JSON_PRETTY_PRINT);

6. json_decode_options()

这个函数设置json_decode()函数的选项。语法:

void json_decode_options ( int $options )

可以设置的选项包括JSON_BIGINT_AS_STRING(将大整数转换为字符串)、JSON_OBJECT_AS_ARRAY(将对象解析为数组)等。

示例:

$json = '{"id":18446744073709551615}';

echo json_decode($json)->id; // 输出-1

json_decode_options(JSON_BIGINT_AS_STRING);

echo json_decode($json)->id; // 输出18446744073709551615

7. json_encode_error_msg()

这个函数返回json_encode()函数的错误信息。语法:

string json_encode_error_msg ( void )

如果没有错误发生,则返回空字符串。可以用于调试JSON编码的错误。

示例:

$data = array(
    'name' => 'John',
    'age' => 30,
    'married' => true,
    'hobbies' => array('reading', 'traveling'),
    'address' => array('city' => 'New York', 'zip' => '10001')
);

$json = json_encode($data, '{"city":"New York"}'); // 错误的选项

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'JSON编码失败:' . json_encode_error_msg();
}
else {
    echo $json;
}

8. json_decode_assoc()

这个函数将JSON字符串解析为关联数组。语法:

mixed json_decode_assoc ( string $json [, int $depth = 512 [, int $options = 0 ]] )

可以指定递归深度和解码选项。如果解码失败,则返回null。

示例:

$json = '{"name":"John","age":30,"married":true,"hobbies":["reading","traveling"],"address":{"city":"New York","zip":"10001"}}';

$data = json_decode_assoc($json);

print_r($data);

输出结果:

Array
(
    [name] => John
    [age] => 30
    [married] => 1
    [hobbies] => Array
        (
            [0] => reading
            [1] => traveling
        )

    [address] => Array
        (
            [city] => New York
            [zip] => 10001
        )

)

9. json_decode_object()

这个函数将JSON字符串解析为对象。语法:

mixed json_decode_object ( string $json [, int $depth = 512 [, int $options = 0 ]] )

可以指定递归深度和解码选项。如果解码失败,则返回null。

示例:

$json = '{"name":"John","age":30,"married":true,"hobbies":["reading","traveling"],"address":{"city":"New York","zip":"10001"}}';

$data = json_decode_object($json);

echo $data->name;

输出结果:

John

10. json_encode_file()

这个函数将PHP数组或对象转换为JSON格式并保存到文件中。语法:

bool json_encode_file ( mixed $value , string $filename [, int $options = 0 [, int $depth = 512 ]] )

参数$value是要编码的PHP数据类型,$filename是要保存到的文件名,$options是编码选项,$depth是递归深度。保存成功返回true,否则返回false。

示例:

$data = array(
    'name' => 'John',
    'age' => 30,
    'married' => true,
    'hobbies' => array('reading', 'traveling'),
    'address' => array('city' => 'New York', 'zip' => '10001')
);

$json = json_encode_file($data, 'data.json', JSON_PRETTY_PRINT);

if ($json) {
    echo '文件保存成功';
}
else {
    echo '文件保存失败';
}