JSON到HTML表?

时间:2018-02-02 21:49:30

标签: html json html-table

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

testthat

更新

我会找到这样的结果;

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

2 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您想要输出$ 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";
}

?>

答案 1 :(得分:0)

我会找到这样的结果;

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

&GT;