我正在 PHP 中处理一个大型 JSON 数据文件 (25 mb)。
现在,我可以获取该文件并检查它的字符串长度,尽管我无法 echo 输出该字符串,但我得到了 24479798。
在 echo strlen() 之后,脚本的其余部分崩溃了,我没有得到进一步的输出,包括 echo "Made it to the Bottom";
为什么我无法让 json_decode 工作?这是脚本内存问题,还是JSON数据中的字符编码问题?我被困住了。
<?php
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
if($result = file_get_contents("test.json")){
echo "GOT IT";
}else{
echo "NO";
}
// This works
echo "Got to here";
// This works
echo strlen($result);
// I get "24479798", which is the file string length
var_dump($diffbotResult);
// Outputs all the JSON data, so I know I have it and it's proper JSON data
$result = json_decode($result, true);
echo json_last_error_msg();
// No output
print_r($result);
// No output
echo "Made it to the bottom";
// Does not echo out anything
?> Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
考虑到您尝试导入大文件,脚本的运行时间比浏览器/服务器所需的时间长,并且会在完成之前超时。像这样的脚本需要在命令行或通过 cron 运行来消除超时。您还可以通过在脚本开头添加
ini_set('memory_limit', '512M');(或根据需要更高的值)来增加内存,以确保它有足够的内存来加载和处理json 文件。