
PHP json_decode() 函数解析 JSON 字符串返回 NULL 的原因及解决方法
在 PHP 开发中,使用 json_decode() 函数解析 JSON 字符串时,经常会遇到返回 NULL 的情况,即使 JSON 字符串看似格式正确。本文将深入探讨此问题,并提供多种解决方案。
问题描述
以下 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 字符串,但它被包含在另一个 JSON 字符串中,且内部的双引号未进行转义。这导致了 JSON 解析器无法正确解析该字符串。
解决方法
以下提供几种解决方法:
立即学习“PHP免费学习笔记(深入)”;
方法一:手动转义双引号
手动将 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); // 输出数组
方法二:使用 PHP 数组构建 JSON
避免字符串拼接,直接使用 PHP 数组构建 JSON 对象,然后使用 json_encode() 函数生成 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],
// ... 其他 avinfo 数据
]
],
// ... 其他 avinfo 数据
],
"format_name" => "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 数据结构比较简单,手动转义或使用数组构建是比较直接有效的方法。如果 JSON 数据结构复杂且动态变化,使用 preg_replace_callback() 可以提供更灵活的处理方式,但需要谨慎编写正则表达式以避免误匹配。 记住始终优先选择更清晰易读的方案。











