如何使用 PHP 将 JSON 解析为 html 表?
Posted
技术标签:
【中文标题】如何使用 PHP 将 JSON 解析为 html 表?【英文标题】:How can I parse JSON into a html table using PHP? 【发布时间】:2014-11-30 12:35:58 【问题描述】:我必须在我的网站上找到一张桌子。并且必须从中获取该表的数据 “http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373” 我已经尝试了很多东西,但没有任何效果......
<!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文件中 什么不起作用?您是否在开发环境中将error_reporting
设置为E_ALL
和display_errors
设置为on
?
你是什么意思?....我猜不是....对php来说还是新手
我想问题是由于安全原因 url_fopen 被设置为禁用。在 PHP 脚本的开头添加 error_reporting(E_ALL);
。并确保在您的 .htaccess 或 php.ini 中将 display_errors
设置为 On
或在您提出请求后查看 PHP 日志文件。
我不知道 php.ini 是什么,也不知道 .htaccess 是什么......,我确实添加了错误报告,但它仍然不起作用。控制台有一个错误,它关于 html 的绘图集以及它是如何未声明的以及关于 US-ASCCI 什么的?
【参考方案1】:
以下代码可用于动态转换整个表格,而无需定义列名。
echo "<table>";
echo "<tr>";
foreach(array_keys($your_array[0]) as $head)
echo "<td>$head</td>";
echo "</tr>";
foreach($your_array as $row)
echo "<tr>";
foreach($row as $col)
echo "<td>$col</td>";
echo "</tr>";
echo "</table>";
return $posicoes;
【讨论】:
【参考方案2】:如果你想要递归方式:
public static function jsonToDebug($jsonText = '')
$arr = json_decode($jsonText, true);
$html = "";
if ($arr && is_array($arr))
$html .= self::_arrayToHtmlTableRecursive($arr);
return $html;
private static function _arrayToHtmlTableRecursive($arr)
$str = "<table><tbody>";
foreach ($arr as $key => $val)
$str .= "<tr>";
$str .= "<td>$key</td>";
$str .= "<td>";
if (is_array($val))
if (!empty($val))
$str .= self::_arrayToHtmlTableRecursive($val);
else
$str .= "<strong>$val</strong>";
$str .= "</td></tr>";
$str .= "</tbody></table>";
return $str;
然后拨打echo YourClass::jsonToDebug($jsonText)
;
我对@987654321@的测试
【讨论】:
最佳答案,因为它是 1) 结构无关和 2) 代码高效(递归)【参考方案3】:为什么不试试呢:
$data = json_decode($json, true);
【讨论】:
解释:第二个参数 (true) 告诉 json_decode() 返回一个关联数组而不是给定数据的对象,如果您不从事 OO 编程,使用它可能更直观。 【参考方案4】:从外部来源获取数据时,首先要做的是了解返回的内容。
也是这样
<?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
)
所以现在您知道您正在处理的是一个 OBJECT,而不是标量值或数组。
所以试试这个代码:-
<?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>";
?>
【讨论】:
哇,非常感谢它终于起作用了 :D 你知道一种让它对位置进行排序的方法吗? 1.2.3.4.5.6.7.8.9.10.11等以此类推【参考方案5】:我猜,问题在于您使用的变量 - $afko
和 $positie
。请尝试以下代码 -
// Cycle through the array
foreach ($data as $stand)
// Output a row
echo "<tr>";
echo "<td> . $stand['afko'] . </td>";
echo "<td> . $stand['positie'] . </td>";
echo "</tr>";
【讨论】:
以上是关于如何使用 PHP 将 JSON 解析为 html 表?的主要内容,如果未能解决你的问题,请参考以下文章
如何将 YouTube JSON 解析为 PHP? [复制]