[英]Create HTML table from JSON array
I've been trying for hours to create a simple table with data from a JSON array. 我已经尝试了几个小时,用JSON数组中的数据创建一个简单的表。
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. 请查看json_string的URL以进行格式化。
$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
您必须在
$array->result
上设置foreach循环
Alternatively, you could add a second parameter to json_decode($json_string, true)
to make it an array instead of an object. 或者,您可以向
json_decode($json_string, true)
添加第二个参数,使其成为数组而不是对象。 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>
Trying to get property of non-object
means one of the ->property
call failed because that property does not exist. Trying to get property of non-object
意味着->property
调用之一失败,因为该属性不存在。 In this case, it's the $o->result that failed. 在这种情况下,$ o-> result失败了。
If you print out content of $array, you can see it's structured like this: 如果打印出$ array的内容,则可以看到它的结构如下:
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>
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.