[英]how to parse json into php
i have a json data retrieved from data base ie 我有一个从数据库检索的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);
and this give me the result
[{"fish_id":"1","name":"first fish update","info":"this is my first fish update","image":"http:\\/\\/www.localhost\\/cafe\\/pics\\/logout (1).gif"}][{{“ fish_id”:“ 1”,“ name”:“第一次鱼类更新”,“ info”:“这是我第一次鱼类更新”,“ image”:“ http:\\ / \\ / www.localhost \\ / cafe \\ / pics \\ / logout(1).gif“}”
but from another page when i call this json data ie 但是从另一个页面,当我调用此json数据即
$input = file_get_contents("http://localhost/fish/fish-json.php");$ input = file_get_contents(“ http://localhost/fish/fish-json.php”);\n$json=json_decode($input);
$ JSON = json_decode($输入);\necho $json->fish_id;
echo $ json-> fish_id;
it give me the error 它给我错误
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注意:尝试在第13行的/var/www/fish/json-to-php.php中获取非对象的属性调用堆栈:0.0005 318764 1. {main}()/ var / www / fish / json-to -php.php:0
Is an array of object, so 是一个对象数组,所以
echo $json[0]->fish_id;
To loop 循环播放
if (!is_array($json)) die('...');
foreach ($json as $key=>$fish)
{
echo $fish->fish_id;
}
Try 尝试
$input = file_get_contents("http://localhost/fish/fish-json.php");
$json=json_decode($input);
echo $json['fish_id'];
try echo $json['fish_id'];
尝试
echo $json['fish_id'];
i might suspect it convert it to an array 我可能会怀疑它将其转换为数组
try this first: 首先尝试:
echo $json;
to check if you got valid json. 检查您是否有有效的json。 Eventually, the php in fish-json.php was not executed.
最终,未执行fish-json.php中的php。
By default json_decode returns an stdClass object. 默认情况下,json_decode返回一个stdClass对象。 You have to give a second parameter for this function TRUE.
您必须为此功能提供第二个参数TRUE。
$json=json_decode($input, TRUE);
echo $json[0]['fish_id'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.