首頁  >  問答  >  主體

無法回顯或解析較長的JSON字串

我正在 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粉116631591P粉116631591179 天前366

全部回覆(1)我來回復

  • P粉043295337

    P粉0432953372024-03-29 00:03:17

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

    回覆
    0
  • 取消回覆