dongsimu4422 2013-11-29 18:07
浏览 38

尝试使用PHP获取JSON。 出现在不同的阵型?

I am trying to Get JSON data using PHP from another page, but its showing up in a weird formation and beside eachother on a single line.

I tried to use json_decode method, but didnt work for me! But how can I make it show up in the correct formation showen below:

This is how I want it to show up, which it does on the Main Page:

{
    "games": "pacman",
    "level":"2",
    "icon":"LINK HERE",
    "topscorer":"3134000",
    "nextlevel":"3",
    "players":[ 21322,
                43131,
                84993,
                8212,
                501421,
                7832126 ]
}

And this is what I get:

{ "games": "pacman", "level":"2", "icon": "LINK HERE", "topscorer": "3134000", "nextlevel": "3", "players": [ 21322, 43171, 84993, 8222, 501421, 7832126 ] }

My PHP Code:

<?php
$page = file_get_contents('WEBSITE-URL-HERE');
echo $page;
?>

Is there any way to make it show up in correct formation just like the one above? Any answer could be help full in this case. Thank you

  • 写回答

3条回答 默认 最新

  • dryb38654 2013-11-29 18:36
    关注

    I know this is kinda crude and I haven't tested it but it was done fast

    a standalone test file

    Leave this by itself as a test to see the output

    <?php
    
    function prettifyJSON($json) {
      static $i = 0;
      if (is_bool($json)) return $json ? 'true' : 'false';
      if (is_null($json)) return 'null';
      if (is_numerical($json)) return $json;
      if (is_string($json)) return '"'.$json.'"';
      if (is_array($json)) {
        $r = array();
        foreach ($json as $v) $r[] = prettifyJSON($v);
        return '['.implode(', ',$r).']';
      }
      if (is_object($json)) {
        $r = array();
        foreach ($json as $k => $v) $r[] = '"'.$k.'": '.prettifyJSON($v).PHP_EOL;
        return '{'.PHP_EOL.implode(', ',$r).'}'.PHP_EOL;
      }
    }
    ob_start();
    echo 'this script is intended for testing purposes';
    
    // assume this is a text file with parsed json in it
    $page = file_get_contents('WEBSITE-URL-HERE');
    
    // let's look at it raw
    echo 'RAW OUTPUT: '.PHP_EOL;
    var_dump($page);
    
    echo 'FORMATTED OUTPUT: '.PHP_EOL;
    echo prettifyJSON(json_decode($page));
    
    echo PHP_EOL.'done formatting';
    echo '<pre>'.ob_get_clean().'</pre>';
    exit;
    

    Please give some feedback and I'll update this to suit your needs

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探