【问题标题】:Trying to get property of non-object json in php试图在php中获取非对象json的属性
【发布时间】:2016-12-27 05:02:15
【问题描述】:

我正在尝试使此代码正常工作,但我不断收到此错误:

试图获取非对象的属性..

我已经验证了我的 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}

任何帮助将不胜感激。

【问题讨论】:

    标签: php json


    【解决方案1】:

    你可以这样试试吗?

    代替

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

    使用

    $teamInformation = json_decode($json, true);
    $success = $teamInformation["success"];

    【讨论】:

    • 我确实尝试过这种方法,它给了我完全相同的错误。
    • 你可以直接回显 $json 并检查其余 2 行的评论
    • [更新] 我再次使用它,它什么也没给我,但好消息是错误不再存在......只是空输出。
    • echo var_dump($json);
    • 我得到了这个:string(205)" {"team":{"tid":"2","name":"Italy","wins":"4","group" :"D","otherteam":[{"name":"Uruguay"},{"name":"England"}]},"success":1}"
    猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多