我正在嘗試從 AWS S3 讀取 json 檔案。
我可以訪問該檔案并列印出 json 值,但它不會讓我對它做任何事情。
它給了我一個錯誤說:“可恢復的致命錯誤:stdClass 類的物件無法轉換為字串”,即使它的型別設定為字串。
我所擁有的如下:
<?php
require "../vendor/autoload.php";
use Aws\S3\S3Client;
$aws_credentials = [
'region' => 'eu-west-1',
'version' => 'latest',
'credentials' => [
'key' => 'xxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx'
]
];
$aws_client = new S3Client($aws_credentials);
$bucket = 'xxxxxxx';
$file_name = 'data.json';
$result = $aws_client->getObject(array(
'Bucket' => $bucket,
'Key' => $file_name
));
$json = (string)$result['Body'];
echo $json; // this outputs the json I want to work with
echo '<br />';
echo gettype($json); // this outputs 'string'
echo '<br />';
echo json_decode($json); // this outputs 'Recoverable fatal error: Object of class stdClass could not be converted to string'
?>
uj5u.com熱心網友回復:
的輸入是json_decode一個字串,但該函式的輸出是一個物件。
然后,您將該輸出傳遞給echo,但echo需要將其轉換為字串,并且不知道如何。
如果我們將輸出分配給一個變數,這可能會更清楚:
echo gettype($json); // this outputs 'string'
echo '<br />';
$object = json_decode($json);
echo gettype($object); // this will output 'object'
var_dump($object); // this will show you what's in the object
echo $object; // this is an error, because you can't echo an object
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/472770.html
