簡體   English   中英

如何在Javascript或PHP中顯示JSON響應

[英]How to display JSON response in Javascript or PHP

我正在網站上設置Coin Warz API,並且API以JSON格式和JSON數據響應的形式發送響應,我正在嘗試使用PHP以表格格式顯示它,但是沒有任何反應。

JSON響應-數組([Success] => 1 [Message] =>剩余996個api調用。

[Data] => Array
    (
        [CoinName] => Bitcoin
        [CoinTag] => BTC
        [BlockCount] => 558860
        [Difficulty] => 5883988430955.4
        [BlockReward] => 12.5
        [IsBlockExplorerOnline] => 1
        [IsExchangeOnline] => 1
        [Algorithm] => SHA-256


        [ExchangeRates] => Array
            (
                [0] => Array
                    (
                        [Exchange] => Bitfinex
                        [ToUSD] => 3653.3
                        [ToBTC] => 1
                        [Volume] => 13083.6930094
                        [TimeStamp] => 1547674251.583
                    )

                [1] => Array
                    (
                        [Exchange] => CEX.io
                        [ToUSD] => 3663.8
                        [ToBTC] => 1
                        [Volume] => 414.26963602
                        [TimeStamp] => 1547674248.653
                    )

                [2] => Array
                    (
                        [Exchange] => itBit
                        [ToUSD] => 3589.99
                        [ToBTC] => 1
                        [Volume] => 2320.923
                        [TimeStamp] => 1547674245.113
                    )

                [3] => Array
                    (
                        [Exchange] => hitbtc
                        [ToUSD] => 3579.68
                        [ToBTC] => 1
                        [Volume] => 22642.04
                        [TimeStamp] => 1547674242.827
                    )

                [4] => Array
                    (
                        [Exchange] => Bitstamp
                        [ToUSD] => 3587.77
                        [ToBTC] => 1
                        [Volume] => 7681.97313751
                        [TimeStamp] => 1547674201.397
                    )

                [5] => Array
                    (
                        [Exchange] => Coinbase
                        [ToUSD] => 3607.29
                        [ToBTC] => 1
                        [Volume] => 0
                        [TimeStamp] => 1547674201.117
                    )

            )

        [BlockTimeInSeconds] => 600
        [HealthStatus] => Healthy
        [Message] => 
    )

PHP代碼-

<?php`$json =file_get_contents("https://www.coinwarz.com/v1/api/coininformation/? 
apikey=6b51849101ca4e4bb353d719546e919c&cointag=BTC");`$data =  json_decode($json, true);` if (count($data->Data)) {
    // Open the table
    echo "<table>";`// Cycle through the loop
    foreach ($data[0]->Data as $idx => $Data) {
// Output a row
        echo "<tr>";
        echo "<td>$Data->CoinName</td>";
        echo "<td>$Data->CoinTag</td>";
        echo "</tr>";
    }`// Close the table
    echo "</table>";
}
?>`

我希望所有值都采用表格格式,例如-[CoinName] [CoinTag] [BlockCount] [Difficulty] [BlockReward]等。

嘗試此希望它能工作,您在數組而非對象中解碼了響應。

$json = file_get_contents("https://www.coinwarz.com/v1/api/coininformation/?apikey=6b51849101ca4e4bb353d719546e919c&cointag=BTC"); 

        $data =  json_decode($json, true);
        if (count($data['Data'])) {
            // Open the table
            echo '<table><tr>
            <td>CoinName</td><td>CoinTag</td>BlockCount<td>Difficulty</td><td>BlockReward</td><td>Algorithm</td>
            </tr>';

            echo "<tr>";
            foreach ($data as $key => $Dataarray) {
    //echo '<pre>'; print_r($Dataarray);exit;

                echo "<td>".$Dataarray['CoinName']."</td>";
                echo "<td>".$Dataarray['CoinTag']."</td>";
                echo "<td>".$Dataarray['BlockCount']."</td>";
                echo "<td>".$Dataarray['Difficulty']."</td>";
                echo "<td>".$Dataarray['BlockReward']."</td>";
                echo "<td>".$Dataarray['Algorithm']."</td>";
            }
            echo "</tr></table>";
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM