douxun4173 2014-12-22 20:49 采纳率: 0%
浏览 22
已采纳

使用php从URL获取JSON对象

I have from url this

[{"user_id":"3932131","username":"DanielDimitrov","count300":"1677134","count100":"239025","count50":"41207","playcount":"17730","ranked_score":"1413977663","total_score":"7355146958","pp_rank":"35848","level":"95.5852","pp_raw":"1582.26","accuracy":"97.88556671142578","count_rank_ss":"42","count_rank_s":"337","count_rank_a":"120","country":"BG","events":[]}]

and my php is this

$json = file_get_contents('url');
    $obj = json_decode($json);
    echo $obj->user_id;
    echo 'Username: '.$obj['username'].'<br>';
    echo 'PP: '.(int)$obj['pp_raw'].'<br>';
    echo 'Level: '.(int)$obj['level'].'<br>';
    echo 'Play count: '.$obj['playcount'].'<br>';

I try to remove the brackets from the url and the code run but i get it with brackets... How can i remove them?

  • 写回答

2条回答 默认 最新

  • drhg24275 2014-12-22 20:59
    关注
    <?php
    $json = '[{"user_id":"3932131","username":"DanielDimitrov","count300":"1677134","count100":"239025","count50":"41207","playcount":"17730","ranked_score":"1413977663","total_score":"7355146958","pp_rank":"35848","level":"95.5852","pp_raw":"1582.26","accuracy":"97.88556671142578","count_rank_ss":"42","count_rank_s":"337","count_rank_a":"120","country":"BG","events":[]}]';
        $in = array("[{", "}]");
        $out = array("{", "}");
        $obj = json_decode(str_replace($in, $out, $json));
        echo $obj->user_id;
        echo 'Username: '.$obj->username.'<br>';
        echo 'PP: '.(int)$obj->pp_raw.'<br>';
        echo 'Level: '.(int)$obj->level.'<br>';
        echo 'Play count: '.$obj->playcount.'<br>';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上