PHP的json_decode()函数为什么会返回NULL而不是预期的数组?

为什么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免费学习笔记(深入)”;

$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而不是预期的数组?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/3167911.html

(0)
上一篇 2025年3月31日 04:29:20
下一篇 2025年3月31日 04:29:34

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论