这个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 解析失败。
看看修复后的:
不需要手动的方法:
用 preg_replace_callback()