这个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); //输出数组
不需要手动的方法:
$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);
我正在用迅捷的语言工作。我不知道这个< code > 2020-07-23t 00:17:06.000 z 的日期格式。 我想按照该格式将转换为 我该怎么办?先谢谢你…
问题内容: 我想我已经找到了错误所在: 我进行调试,得到以下结果(我输入了555和欧元): 因此看来,用于解码JSON对象的PHP函数在某处做错了。有什么帮助吗? 问题答案: Google给您的响应不是有效的JSON,因为未引用标签。您必须自己解析。 输出:
问题内容: 由于空字符串是Go的零/默认值,因此我决定将所有此类字段定义为。例如 如果该值不适用于该特定字段,则我正在发送数据的应用程序期望使用null而不是空字符串。 这是正确的方法吗?或者有人可以向我指出比这更好的方法。 问题答案: 在json包文档中: 指针值编码为指向的值。nil指针编码为空JSON对象。 因此,您可以存储指向字符串的指针,如果不为nil,则将其编码为字符串;如果为nil,
问题内容: 在.NET Framework 的参考中,使用type 声明请求类型。 在RFC 2616 中,声明了所有HTTP请求方法(例如POST,GET,PUT,DELETE …)。 .NET 和类中也存在类似的行为。 Java在方法上有类似的方法。 这些语言设计者为什么不考虑为这些HTTP方法实现枚举? 你有好主意吗? 问题答案: RFC 2616 链接的第一句话(添加了重点): HTTP
我使用的是Laravel,并有一个查询,它使用列的进行选择: 我已经安装了mysqldn,AFAIK Laravel使用。 但是是一个字符串,不管列是什么类型。(它是一个整数列) 另外,如果我这样做: < code>foo_bar以整数形式返回。
问题内容: 我有一个$ .ajax()请求,其dataType设置为“ json”。服务器返回的JSON具有正确的mime类型“ application / json”。但是,我的jqXHR对象中的responseText始终是字符串。我究竟做错了什么?这是应该如何工作的吗? 这是我拨打电话的方式: 所以我必须做一个才能得到一个实际的对象。这似乎没有必要,因为$ .ajax()应该根据文档自动转换