無法回顯或解析較長的JSON字串
P粉116631591
P粉116631591 2024-03-28 09:27:52
0
1
363

我正在 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

?>

P粉116631591
P粉116631591

全部回覆(1)
P粉043295337

考慮到您嘗試匯入大文件,腳本的運行時間比瀏覽器/伺服器所需的時間長,並且會在完成之前逾時。像這樣的腳本需要在命令列或透過 cron 運行來消除逾時。您還可以透過在腳本開頭添加ini_set('memory_limit', '512M'); (或根據需要更高的值)來增加內存,以確保它有足夠的內存來加載和處理json文件。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!