訪問JSON我想讀的頁面http://mattrb.com/txt.txt用PHP
例如在JSON數據,讓我們說我想要得到的名稱爲「妙蛙種子。」我有這樣的代碼:
<?php
$file = file_get_contents("http://mattrb.com/txt.txt");
$json = json_decode($file);
echo $json->1->name;
?>
此代碼會導致php無法加載。這是因爲你不能使用數字嗎?接下來我嘗試了這個:
<?php
$file = file_get_contents("http://mattrb.com/txt.txt");
$json = json_decode($file);
$num = 1;
echo $json->$num->name;
?>
這允許php加載,但仍然沒有返回任何內容。我究竟做錯了什麼?
是$ json這是一個對象嗎?使用var_dump來知道你從這個文件得到的是什麼 –
嘗試'print json_last_error();'之前。 – mario