2016-12-27 70 views
-5

我试图让这段代码的工作,但我不断收到此错误:试图让非对象JSON的财产在PHP

试图让非对象的属性..

我验证了我的json对象,但仍然没有运气。我搜索了这个问题,但没有找到任何解决方案。我的代码第一部分,其创建JSON和打印:

$response['team']['tid'] = $teamData['tid']; 
$response['team']['name'] = $teamData['name']; 
$response['team']['wins'] = $teamData['wins']; 
$response['team']['group'] = $teamData['tgroup']; 
$otherTeamNames = $db->query("SELECT name from teams where tgroup= '$teamData[4]'"); 

while($values = $otherTeamNames->fetch(PDO::FETCH_ASSOC)){ 
    if($values['name'] != $teamData['name']) 
    $response['team']['otherteam'][] = $values; 

} 
$response['success'] = 1; 
echo json_encode($response); 

第二部分将在JSON

$json = file_get_contents('http://localhost/pract/getSingleTeamById.php', false, $context); 

$teamInformation = json_decode($json); 
$success = $teamInformation->{"success"}; 

解码之前的$json解码输出:

{"team":{"tid":"2","name":"Italy","wins":"4","group":"D","otherteam":[{"name":"Uruguay"},{"name":"England"}]},"success":1} 

任何帮助将不胜感激。

回答

0

你能试试吗?

代替

$ teamInformation = json_decode($ JSON);
$ success = $ teamInformation - > {“success”};

使用

$ teamInformation = json_decode($ JSON,真正的);
$ success = $ teamInformation [“success”];

+0

我确实尝试过这种方法,它给了我完全相同的错误。 –

+0

你可以只是回声$ json和检查评论休息2行 –

+0

[已更新]我再次使用它,它给了我什么,但好消息是错误不再存在...只是空输出。 –