我正在用 Laravel 開發一個 api,我必須在 url 中獲取 json 檔案的資料,這是我的代碼:
public function index(){
$json_file=file_get_contents("url that I'm using");
$json_str = json_decode($json_file);
echo $json_str;
}
JSON 檔案:
{
"success":true,
"clients": [
[
"name": "Tyler Durden",
"job": "leader of the ..."
],
]
}
但是當我訪問呼叫 index() 的路由時,我收到此錯誤:
Object of class stdClass could not be converted to string
我想得到任何一對客戶并保存他們,有人可以幫助我嗎?
uj5u.com熱心網友回復:
您不能使用echo, 列印物件來列印您可以使用的物件
print_r($json_str);
var_dump($json_str);
在你的情況下,你只需要一個return宣告
return $json_str;
在 Laravel 中,您也可以這樣做
return response()->json($json_str);
uj5u.com熱心網友回復:
您正在嘗試列印 json 而不是字串。echo 只會列印一個字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/312814.html
