【问题标题】:Create HTML table from JSON array从 JSON 数组创建 HTML 表
【发布时间】:2014-08-02 11:01:58
【问题描述】:

我已经尝试了几个小时来创建一个包含来自 JSON 数组的数据的简单表。

下面是一个例子。我不知道为什么这段代码不起作用。错误是:

“试图获取非对象的属性”。

$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>

请查看 json_string 的 URL 以进行格式化。

【问题讨论】:

标签: php html json html-table


【解决方案1】:

或者,您可以向json_decode($json_string, true) 添加第二个参数,使其成为数组而不是对象。考虑这个例子:

<?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>

【讨论】:

    【解决方案2】:
    $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>
    

    这应该可行。您必须在 $array-&gt;result 处设置 foreach 循环

    【讨论】:

      【解决方案3】:

      Trying to get property of non-object 表示 -&gt;property 调用之一失败,因为该属性不存在。在这种情况下,是 $o->result 失败了。

      如果你打印出 $array 的内容,你可以看到它的结构是这样的:

      print_r($array);
      

      输出:

      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
                      )
                  ...
      

      现在你可以按照这个结构来获取内部对象:

      <?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";
      

      输出:

      <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>
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-23
        • 2021-07-24
        • 2014-06-29
        相关资源
        最近更新 更多