php,这个json格式的字符串,为什么json_decode()结果是NULL,而不是预期的数组?
这个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
问题出在哪里?
回复
1个回答

test
2024-07-04
"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); //输出数组
不需要手动的方法:
$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()
$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);
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容