【问题标题】:JSON to HTML Table?JSON 到 HTML 表?
【发布时间】:2018-07-13 11:09:20
【问题描述】:

如何从这个 url 的 json 输出生成 html 表格?

http://megagrup.site/entegrasyon/hepsiburada.php

更新

我会找到这样的结果;

<?php
$json=file_get_contents("url");
$data =  json_decode($json);

if (count($data->listings)) {
    // Open the table
    echo "<table>";

    // Cycle through the array
    foreach ($data->listings as $idx => $stand) {

        // Output a row
        echo "<tr>";
        echo "<td>".$stand->abc."</td>";
        echo "<td>".$stand->def."</td>";
        echo "</tr>";
    }

    // Close the table
    echo "</table>";
}

【问题讨论】:

    标签: html json html-table


    【解决方案1】:

    如果我正确理解您的问题,您希望输出 $stand 对象的属性。您需要一个额外的循环来迭代属性。

    <?php
    $json=file_get_contents("http://megagrup.site/entegrasyon/hepsiburada.php");
    $data =  json_decode($json);
    
    if (count($data->listings)) {
        // Open the table
        echo "<table>\n";
    
        // Cycle through the array
        foreach ($data->listings as $idx => $stand) {
    
            // Output a row
            echo "  <tr>\n";
    
            // Output a cell for each property of the $stand object
            foreach ($stand as $key => $value) {
                echo "    <td>" . $value . "</td>\n";
            }
    
            echo "  </tr\n";
        }
    
        // Close the table
        echo "</table>\n";
    }
    
    ?>
    

    【讨论】:

      【解决方案2】:

      我会找到这样的结果;

      <?php
      $json=file_get_contents("url");
      $data =  json_decode($json);
      
      if (count($data->listings)) {
          // Open the table
          echo "<table>";
      
          // Cycle through the array
          foreach ($data->listings as $idx => $stand) {
      
              // Output a row
              echo "<tr>";
              echo "<td>".$stand->abc."</td>";
              echo "<td>".$stand->def."</td>";
              echo "</tr>";
          }
      
          // Close the table
          echo "</table>";
      }
      

      ?>

      【讨论】:

      • 我编辑了您的问题以包含此信息。我建议你删除这个答案。
      猜你喜欢
      • 2015-08-17
      • 2014-06-13
      • 2013-08-02
      • 1970-01-01
      • 2017-04-28
      • 2014-06-16
      • 2016-03-13
      • 2017-08-11
      相关资源
      最近更新 更多