【发布时间】:2013-04-14 00:59:31
【问题描述】:
我正在尝试通过 enjin API 显示此 JSON 响应中的所有用户名列表,但我收到此错误:
警告:为 foreach() 提供的参数无效
代码
<?php
$s = curl_init();
curl_setopt($s, CURLOPT_URL, 'http://oceanix.enjin.com/api/get-users');
$output = curl_exec($s);
curl_close($s);
$decodedJson = json_decode($output);
?>
<table>
<tr>
<th>username</th>
</tr>
<?php
foreach ($decodedJson as $username) { ?>
<tr>
<td><?php echo $username->username; ?></td>
</tr>
<?php
}?>
</table>
感谢任何帮助。谢谢
【问题讨论】:
-
请在此处不解码打印 $output。
-
"340341:{"username":"Nord","forum_post_count":"0","forum_votes":"0","forum_up_votes":"0","forum_down_votes":"0 ","lastseen":"1357188768","datejoined":"1357188768"},
-
这看起来不像是有效的 JSON。您能否将整个输出复制并粘贴到问题body 中?你能用
var_dump()之类的东西发布$decodedJson的内容吗? -
在解码之前用 { 和 } 包围您的输入 JSON。另外,
var_dump( $decodedJson )——它可能是 NULL。 -
@J.D.Pace JSON 已经被正确包围了......