duanjia8215 2014-08-16 12:45
浏览 11
已采纳

JSON在PHP中不显示错误

I'm looking to display Json from a url (http://pastebin.com/raw.php?i=MSyA8CBZ). Then, I want the "steamid" to be displayed. Currently, I am using this code, but it's not working:

<?php 
$json = file_get_contents('http://pastebin.com/raw.php?i=MSyA8CBZ');
$obj = json_decode($json);
echo $obj->response->players->steamid; 
?>

I get the error: Fatal error: Cannot use object of type stdClass as array in /home/u434538987/public_html/testjson.php on line 4 What is wrong with my code?

  • 写回答

1条回答 默认 最新

  • dounabi6295 2014-08-16 12:55
    关注

    As mentioned in the comment, players is an array, is you could have seen if you had done print_r($obj) or even saw what the URL you load produced. So you should do :

    $obj->response->players[0]->steamid;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部