我用json_encode($response)
列出了目录中的所有文件。由于它在$response
数组中有近8000条记录,因此当显示在页面中时,它返回null
。
我对这个问题有研究。到目前为止,我找不到任何解决方案。
这是我的代码:
$response['content'] = Files::$output;
echo json_encode($response);
Files::$output` /* this will return the list of files in the html format. */
当我尝试echo json_last_error_msg();
时,它返回:
格式错误的 UTF-8 字符,可能编码不正确
JSON 在通过 json_encode
编码时只能接受有效的 UTF 字符。
很有可能,您有格式错误的输入(如错误消息所述),这很可能是Windows代码页的某种变体。
为了确保您的数组没有无效的 JSON 数据,您可以在数据库层转换它(假设您使用的是数据库),或者执行以下操作(这假设您的格式不正确latin1
/ISO-8859-1
是导致错误数据的原因):
foreach ($row as $key => &$value) {
if ( ! mb_check_encoding($value, "UTF-8")) {
// assume base is latin1; your mileage may vary
$value = mb_convert_encoding($value, "UTF-8", "ISO-8859-1");
}
}
你不能真的强迫json_encode
解析你的无效数据,所以你将不得不把你的数据集调整成有效的UTF-8。