如何解析json到php

hun*_*ter 2 php

我有一个从数据库中检索到的json数据,即

$result=array();
$query="SELECT * FROM fish";
$result1 = mysql_query($query, $conn);
while ($table = mysql_fetch_array($result1, MYSQL_ASSOC)){
   $result[]=$table;
}
   echo json_encode($result);
Run Code Online (Sandbox Code Playgroud) 这给了我结果
[{"fish_id":"1","name":"first fish update","info":"this is my first fish update","image":"http:\/\/www.localhost\/cafe\/pics\/logout (1).gif"}]

但是当我调用这个json数据时,从另一个页面,即

$input = file_get_contents("http://localhost/fish/fish-json.php");
$json=json_decode($input);
echo $json->fish_id;

它给了我错误

Notice: Trying to get property of non-object in /var/www/fish/json-to-php.php on line 13 Call Stack: 0.0005 318764 1. {main}() /var/www/fish/json-to-php.php:0

ajr*_*eal 5

是一个对象数组,所以

echo $json[0]->fish_id;
Run Code Online (Sandbox Code Playgroud)

循环

if (!is_array($json)) die('...');
foreach ($json as $key=>$fish)
{
  echo $fish->fish_id;
}
Run Code Online (Sandbox Code Playgroud)