Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
3  
foreach($array->result as $o) and $o->TimeStamp? And you should also use echo –  asprin Jun 12 at 7:47
    
If you want to use Jquery for this this Here are the links 1- zachhunter.com/2010/04/json-objects-to-html-table , 2- stackoverflow.com/questions/1051061/… –  zafus_coder Jun 12 at 8:08

3 Answers 3

up vote 0 down vote accepted

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>
share|improve this answer
$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

share|improve this answer

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>
...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.