当前位置: 首页 > 面试题库 >

如何使用PHP将JSON解析为html表?

丁志勇
2023-03-14
问题内容

我必须在我的网站上找到一张桌子。并且必须从“” 获取此表的数据,我已经尝试了很多方法,但是没有用。 ..

 <!DOCTYPE html>
  <html>
   <head>
    <script type="text/javascript" 
       src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   </head>
   <body>
    <?php
    $json=file_get_contents("http://west.basketball.nl/db/json
    /stand.pl?szn_Naam=2014-2015&cmp_ID=373");
            $data =  json_decode($json);

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

            // Cycle through the array
            foreach ($data as $stand) {

                // Output a row
                echo "<tr>";
                echo "<td>$afko</td>";
                echo "<td>$positie</td>";
                echo "</tr>";
            }

            // Close the table
            echo "</table>";
        }
    ?>
  </body>
</html>

问题答案:

从外部来源获取数据时,要做的第一件事就是了解所返回的内容。

也是

<?php
$json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
$data =  json_decode($json);

print_r($data);

结果:

stdClass Object
(
    [stand] => Array
        (
            [0] => stdClass Object
                (
                    [afko] => Risne Stars HS 1
                    [ID] => 2091
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 100.0
                    [punten] => 6
                    [tegenscore] => 149
                    [eigenscore] => 191
                    [datum] => 2014-10-05
                    [saldo] => 42
                    [team] => Risne Stars Heren 1
                    [positie] => 1
                )

            [1] => stdClass Object
                (
                    [afko] => D.B.V. Arriba HS 2
                    [ID] => 1813
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 0.0
                    [punten] => 0
                    [tegenscore] => 116
                    [eigenscore] => 102
                    [datum] => 2014-10-05
                    [saldo] => -14
                    [team] => D.B.V. Arriba Heren 2
                    [positie] => 10
                )

            [2] => stdClass Object
                (
                    [afko] => The Valley Bucketeers HS 2
                    [ID] => 2430
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 0.0
                    [punten] => 0
                    [tegenscore] => 177
                    [eigenscore] => 70
                    [datum] => 2014-10-05
                    [saldo] => -107
                    [team] => The Valley Bucketeers Heren 2
                    [positie] => 11
                )

            [3] => stdClass Object
                (
                    [afko] => Uitsmijters HS 2
                    [ID] => 2143
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 100.0
                    [punten] => 4
                    [tegenscore] => 79
                    [eigenscore] => 161
                    [datum] => 2014-10-05
                    [saldo] => 82
                    [team] => Uitsmijters Heren 2
                    [positie] => 2
                )

            [4] => stdClass Object
                (
                    [afko] => Picker Reds HS 1
                    [ID] => 2056
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 66.7
                    [punten] => 4
                    [tegenscore] => 193
                    [eigenscore] => 184
                    [datum] => 2014-10-05
                    [saldo] => -9
                    [team] => Picker Reds Heren 1
                    [positie] => 3
                )

            [5] => stdClass Object
                (
                    [afko] => Peatminers HS 2
                    [ID] => 6247
                    [status] => Actief
                    [gespeeld] => 1
                    [percentage] => 100.0
                    [punten] => 2
                    [tegenscore] => 36
                    [eigenscore] => 64
                    [datum] => 2014-10-05
                    [saldo] => 28
                    [team] => Peatminers Heren 2
                    [positie] => 4
                )

            [6] => stdClass Object
                (
                    [afko] => Jolly Jumpers HS 1
                    [ID] => 1994
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 50.0
                    [punten] => 2
                    [tegenscore] => 103
                    [eigenscore] => 119
                    [datum] => 2014-10-05
                    [saldo] => 16
                    [team] => Jolly Jumpers Heren 1
                    [positie] => 5
                )

            [7] => stdClass Object
                (
                    [afko] => TONEGO '65 HS 2
                    [ID] => 2120
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 50.0
                    [punten] => 2
                    [tegenscore] => 107
                    [eigenscore] => 122
                    [datum] => 2014-10-05
                    [saldo] => 15
                    [team] => TONEGO '65 Heren 2
                    [positie] => 6
                )

            [8] => stdClass Object
                (
                    [afko] => Amical HS 2
                    [ID] => 1791
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 180
                    [eigenscore] => 195
                    [datum] => 2014-10-05
                    [saldo] => 15
                    [team] => Amical Heren 2
                    [positie] => 7
                )

            [9] => stdClass Object
                (
                    [afko] => S.V.Z.W. HS 2
                    [ID] => 5526
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 174
                    [eigenscore] => 151
                    [datum] => 2014-10-05
                    [saldo] => -23
                    [team] => S.V.Z.W. Heren 2
                    [positie] => 8
                )

            [10] => stdClass Object
                (
                    [afko] => Twente Buzzards HS 3
                    [ID] => 2294
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 196
                    [eigenscore] => 151
                    [datum] => 2014-10-05
                    [saldo] => -45
                    [team] => Twente Buzzards Heren 3
                    [positie] => 9
                )

        )

    [nummer] => OHS2C
    [version] => 1.0
    [aantal_teams] => 11
    [id] => 373
    [seizoen] => 2014-2015
    [naam] => Oost Afdeling Heren Senioren 2e klasse C
    [gewijzigd] => 2014-10-05 18:34:25
)

因此,现在您知道您要处理的是对象,而不是标量值或数组。

因此,请尝试以下代码:-

<?php
    $json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
    $data =  json_decode($json);

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

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

            // Output a row
            echo "<tr>";
            echo "<td>$stand->afko</td>";
            echo "<td>$stand->positie</td>";
            echo "</tr>";
        }

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


 类似资料:
  • 问题内容: 如果我们将Apache Web服务器设置为将Apache配置为将 所有 HTML 处理为PHP,是否存在安全性/性能方面的顾虑?我专门指的是: 我当时需要将一些PHP逻辑添加到一些HTML文件中;理想情况下,我不必将文件名更改为(保持的页面排名等)。 这与以下问题有关:httpd AddType指令 编辑: 从下面的现有答案/评论中,社区似乎建议使用重定向或仅针对特定的HTML文件。约

  • 问题内容: 如何解析HTML / XML并从中提取信息? 问题答案: 本机XML扩展 我更喜欢使用本机XML扩展]之一,因为它们与PHP捆绑在一起,通常比所有第3方库都快,并为我提供了所需的所有标记控制权。 DOM DOM扩展使您可以使用PHP 5通过DOM API通过XML文档进行操作。它是W3C的Document Object Model Core Level 3的实现,它是一种平台和语言无关

  • 问题内容: 我试图使用PHP解析JSON文件。但是我现在被困住了。 这是我的JSON文件的内容: 到目前为止,这是我尝试过的: 但是,因为我不知道的名字(例如,)和所有可用键和值(如,)事前,我想我需要创建一些foreach循环。 我希望为此举一个例子。 问题答案: 要遍历多维数组,可以使用RecursiveArrayIterator 输出:

  • 我正在尝试将JSON转换为GSON,我不确定这是最好的结构。 所有响应都由代码、消息和数据结构组成。但数据的内部结构可能会有所不同。 这是我的回应对象

  • 本文向大家介绍使用PHP的HTML DOMDocument解析HTML,包括了使用PHP的HTML DOMDocument解析HTML的使用技巧和注意事项,需要的朋友参考一下 可以通过以下代码获取<div>标记内的<div>标记内的文本,其中<div>内的class =“ main”内- 示例 输出结果 这将产生以下输出-

  • 问题内容: 我有一个包含1-n纬度/经度记录的JSON字符串。看起来像这样: 什么是解析此值以获取经纬度值循环的好方法?转义的双引号是否会影响解析? 谢谢亚历克斯 问题答案: 打印…