登录
首页 >  文章 >  php教程

PHP的json_decode()函数为何返回NULL而不是数组?

时间:2025-03-27 18:45:40 404浏览 收藏

本文探讨了PHP `json_decode()` 函数返回 NULL 而不是数组的常见原因及解决方法。由于JSON字符串中包含未转义的双引号,导致`json_decode()` 函数解析失败,返回 NULL。文章提供了三种解决方法:手动转义JSON字符串中的双引号、使用PHP数组生成JSON字符串以及利用`preg_replace_callback()` 函数自动转义双引号。 通过这三种方法,可以确保`json_decode()` 函数正确解析JSON字符串并返回预期的数组结果。 这对于PHP开发者处理JSON数据至关重要。

为什么php的json_decode()函数解析这个json字符串时返回null而不是预期的数组?

在使用php解析json字符串时,有时候会遇到json_decode()函数返回null的情况。根据提供的代码示例:

$php_input='{"key":"ao_1/f9pbbnam5_0_230502100035.mp3","fname":"ao_1/f9pbbnam5_0_230502100035.mp3","fsize":"234144","avinfo":"{"attachedpic":null,"audios":[{"disposition":{"attached_pic":0},"avg_frame_rate":"0/0","bit_rate":"96000","channels":2,"codec_long_name":"mp3 (mpeg audio layer 3)","codec_name":"mp3","codec_time_base":"1/16000","codec_type":"audio","duration":"19.512000","index":0,"nb_frames":"","profile":"","r_frame_rate":"0/0","sample_fmt":"s16p","sample_rate":"16000","start_time":"0.000000","tags":{}}],"maxab":96000,"subtitles":null,"videos":null,"audio":{"disposition":{"attached_pic":0},"avg_frame_rate":"0/0","bit_rate":"96000","channels":2,"codec_long_name":"mp3 (mpeg audio layer 3)","codec_name":"mp3","codec_time_base":"1/16000","codec_type":"audio","duration":"19.512000","index":0,"nb_frames":"","profile":"","r_frame_rate":"0/0","sample_fmt":"s16p","sample_rate":"16000","start_time":"0.000000","tags":{}},"format":{"bit_rate":"96000","duration":"19.512000","format_long_name":"mp2/3 (mpeg audio layer 2/3)","format_name":"mp3","nb_streams":1,"size":"234144","start_time":"0.000000","tags":{}},"subtitle":null,"video":null}","format_name":"mp3","bit_rate":"96000","duration":"19.512000","ext":".mp3"}';

$arr_post=json_decode($php_input,true);
var_dump($arr_post); //输出null

这里的问题出在avinfo的值中,原本应该是一个字符串,但代码里包含了没有转义的双引号,导致json解析失败。

修正后的代码如下:

$php_input='{"key":"ao_1/f9pbbnam5_0_230502100035.mp3","fname":"ao_1/f9pbbnam5_0_230502100035.mp3","fsize":"234144","avinfo":"{\"attachedpic\":null,\"audios\":[{\"disposition\":{\"attached_pic\":0},\"avg_frame_rate\":\"0/0\",\"bit_rate\":\"96000\",\"channels\":2,\"codec_long_name\":\"mp3 (mpeg audio layer 3)\",\"codec_name\":\"mp3\",\"codec_time_base\":\"1/16000\",\"codec_type\":\"audio\",\"duration\":\"19.512000\",\"index\":0,\"nb_frames\":\"\",\"profile\":\"\",\"r_frame_rate\":\"0/0\",\"sample_fmt\":\"s16p\",\"sample_rate\":\"16000\",\"start_time\":\"0.000000\",\"tags\":{}}],\"maxab\":96000,\"subtitles\":null,\"videos\":null,\"audio\":{\"disposition\":{\"attached_pic\":0},\"avg_frame_rate\":\"0/0\",\"bit_rate\":\"96000\",\"channels\":2,\"codec_long_name\":\"mp3 (mpeg audio layer 3)\",\"codec_name\":\"mp3\",\"codec_time_base\":\"1/16000\",\"codec_type\":\"audio\",\"duration\":\"19.512000\",\"index\":0,\"nb_frames\":\"\",\"profile\":\"\",\"r_frame_rate\":\"0/0\",\"sample_fmt\":\"s16p\",\"sample_rate\":\"16000\",\"start_time\":\"0.000000\",\"tags\":{}},\"format\":{\"bit_rate\":\"96000\",\"duration\":\"19.512000\",\"format_long_name\":\"mp2/3 (mpeg audio layer 2/3)\",\"format_name\":\"mp3\",\"nb_streams\":1,\"size\":\"234144\",\"start_time\":\"0.000000\",\"tags\":{}},\"subtitle\":null,\"video\":null}","format_name":"mp3","bit_rate":"96000","duration":"19.512000","ext":".mp3"}';

$arr_post=json_decode($php_input,true);
var_dump($arr_post); //输出数组

如果不想手动修正json字符串,可以直接使用php数组来生成json:

$data = [
    "key" => "ao_1/f9pbbnam5_0_230502100035.mp3",
    "fname" => "ao_1/f9pbbnam5_0_230502100035.mp3",
    "fsize" => "234144",
    "avinfo" => [
        "attachedpic" => null,
        "audios" => [
            [
                "disposition" => ["attached_pic" => 0],
                "avg_frame_rate" => "0/0",
                "bit_rate" => "96000",
                "channels" => 2,
                "codec_long_name" => "mp3 (mpeg audio layer 3)",
                "codec_name" => "mp3",
                "codec_time_base" => "1/16000",
                "codec_type" => "audio",
                "duration" => "19.512000",
                "index" => 0,
                "nb_frames" => "",
                "profile" => "",
                "r_frame_rate" => "0/0",
                "sample_fmt" => "s16p",
                "sample_rate" => "16000",
                "start_time" => "0.000000",
                "tags" => [],
            ]
        ],
        // ... 其他数据
    ],
    "format_name" => "mp3",
    "bit_rate" => "96000",
    "duration" => "19.512000",
    "ext" => ".mp3",
];

$php_input = json_encode($data);

$arr_post = json_decode($php_input, true);
var_dump($arr_post); //输出数组

另外,还可以使用preg_replace_callback()函数来自动转义avinfo的值中的双引号:

$php_input = '{"key":"ao_1/f9pbbnam5_0_230502100035.mp3","fname":"ao_1/f9pbbnam5_0_230502100035.mp3","fsize":"234144","avinfo":"{"AttachedPic":null,"Audios":[{"Disposition":{"attached_pic":0},"avg_frame_rate":"0/0","bit_rate":"96000","channels":2,"codec_long_name":"MP3 (MPEG audio layer 3)","codec_name":"mp3","codec_time_base":"1/16000","codec_type":"audio","duration":"19.512000","index":0,"nb_frames":"","profile":"","r_frame_rate":"0/0","sample_fmt":"s16p","sample_rate":"16000","start_time":"0.000000","tags":{}}],"MaxAB":96000,"Subtitles":null,"Videos":null,"audio":{"Disposition":{"attached_pic":0},"avg_frame_rate":"0/0","bit_rate":"96000","channels":2,"codec_long_name":"MP3 (MPEG audio layer 3)","codec_name":"mp3","codec_time_base":"1/16000","codec_type":"audio","duration":"19.512000","index":0,"nb_frames":"","profile":"","r_frame_rate":"0/0","sample_fmt":"s16p","sample_rate":"16000","start_time":"0.000000","tags":{}},"format":{"bit_rate":"96000","duration":"19.512000","format_long_name":"MP2/3 (MPEG audio layer 2/3)","format_name":"mp3","nb_streams":1,"size":"234144","start_time":"0.000000","tags":{}},"subtitle":null,"video":null}","format_name":"mp3","bit_rate":"96000","duration":"19.512000","ext":".mp3"}';

$php_input_fixed = preg_replace_callback(
    '/"avinfo":"(.*?)"/',
    function ($matches) {
        return '"avinfo":"' . str_replace('"', '\\"', $matches[1]) . '"';
    },
    $php_input
);

$arr_post = json_decode($php_input_fixed, true);
var_dump($arr_post);

通过上述方法,可以成功解析json字符串并得到预期的数组结果。

到这里,我们也就讲完了《PHP的json_decode()函数为何返回NULL而不是数组?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>