2

I've been trying for hours to create a simple table with data from a JSON array.

Below is an example. I don't know why this code doesn't work. The error is:

"Trying to get property of non-object".

$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5");

$array = json_decode($json_string);  
?>

<table><tr><th>
<?php foreach($array as $o): ?>

<tr>
  <td><?php $o->result->TimeStamp ?></td>
</tr>
<?php endforeach; ?>
</table>

Please look at the URL for json_string for formating.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
leptoon
  • 21
  • 1
  • 1
  • 4
  • 3
    `foreach($array->result as $o)` and `$o->TimeStamp`? And you should also use `echo` – asprin Jun 12 '14 at 07:47
  • If you want to use Jquery for this this Here are the links 1- http://www.zachhunter.com/2010/04/json-objects-to-html-table/ , 2- http://stackoverflow.com/questions/1051061/convert-json-array-to-an-html-table-in-jquery – zafus_coder Jun 12 '14 at 08:08

3 Answers3

0
$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5");

$array = json_decode($json_string);  
?>

<table>
<?php foreach($array->result as $o)
{
echo "<tr>
  <td>".$o->TimeStamp."</td>
</tr>";
} ?>
</table>

This should work. You have to set the foreach Loop at the $array->result

schnawel007
  • 3,982
  • 3
  • 19
  • 27
0

Alternatively, you could add a second parameter to json_decode($json_string, true) to make it an array instead of an object. Consider this example:

<?php
$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5");
$array = json_decode($json_string, true);

?>

<table border="1" cellpadding="10">
    <thead><tr><th>Timestamp</th></tr></thead>
    <tbody>
    <?php foreach($array['result'] as $key => $value): ?>
        <tr>
            <td><?php echo $value['TimeStamp']; ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
user1978142
  • 7,946
  • 3
  • 17
  • 20
0

Trying to get property of non-object means one of the ->property call failed because that property does not exist. In this case, it's the $o->result that failed.

If you print out content of $array, you can see it's structured like this:

print_r($array);

output:

stdClass Object
(
    [success] => 1
    [message] => 
    [result] => Array
        (
            [0] => stdClass Object
                (
                    [Id] => 10044
                    [TimeStamp] => 2014-06-12T04:36:32.227
                    [Quantity] => 3
                    [Price] => 2.9E-5
                    [Total] => 8.7E-5
                    [FillType] => FILL
                    [OrderType] => BUY
                )

            [1] => stdClass Object
                (
                    [Id] => 10040
                    [TimeStamp] => 2014-06-12T04:23:22.683
                    [Quantity] => 49.9
                    [Price] => 2.5E-5
                    [Total] => 0.0012475
                    [FillType] => PARTIAL_FILL
                    [OrderType] => SELL
                )
            ...

Now you can follow this structure to get the inner objects:

<?php
$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5");

echo "<table>\n";
$array = json_decode($json_string);  
foreach ($array->result as $o) {
    echo "<tr><td>$o->TimeStamp</td></tr>\n";
}
echo "</table>\n";

outputs:

<table>
<tr><td>2014-06-12T04:36:32.227</td></tr>
<tr><td>2014-06-12T04:23:22.683</td></tr>
<tr><td>2014-06-12T04:01:43.217</td></tr>
<tr><td>2014-06-12T02:02:29.03</td></tr>
...
Fabricator
  • 12,722
  • 2
  • 27
  • 40