【问题标题】:How to read a json file into html table php?如何将 json 文件读入 html 表 php?
【发布时间】:2016-08-26 20:12:00
【问题描述】:

好的,这是我第一次在这个网站上发帖。我需要一点帮助,我正在尝试将 json API 中的数据添加到 html 表中。这是我目前所拥有的。

<?php

$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
var_dump(json_decode($json));

<?

这是我的输出:

object(stdClass)#1 (2) { ["status"]=&gt; string(3) "200" ["response"]=&gt; object(stdClass)#2 (5) { ["onlineId"]=&gt; string(6) "x0--II" ["avatar"]=&gt; string(77) "http://static-resource.np.community.playstation.net/avatar_m/SCEI/I0053_m.png" ["plus"]=&gt; int(0) ["aboutMe"]=&gt; string(0) "" ["trophies"]=&gt; object(stdClass)#3 (6) { ["trophyLevel"]=&gt; int(3) ["progress"]=&gt; int(7) ["platinum"]=&gt; int(0) ["gold"]=&gt; int(2) ["silver"]=&gt; int(6) ["bronze"]=&gt; int(19) } } }

我想显示图像以及所有值。感谢您的帮助!

【问题讨论】:

  • 解码 json 后,它就像 PHP 中的任何其他数据结构一样。您访问结构中的数据的方式与访问任何其他结构的方式完全相同。
  • 是的,如果您想像访问数组一样访问它,请使用json_decode($json, true)。否则,您需要使用 -&gt; 作为属性。你刚刚环绕&lt;img src=""&gt;标签的图像。

标签: php html css json


【解决方案1】:

您可以将 json_decode 数据放入 PHP 变量中,并像普通 PHP 对象数组一样访问它

这是一个简单的例子:

<?php

$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
$content = json_decode($json);

echo "ID: ".$content->response->onlineId."<br />";
echo '<img src="'.$content->response->avatar.'" />';

echo "<table border=1>";
foreach ($content->response->trophies as $key => $value){
    echo "<tr>
            <td>".$key."</td>
            <td>".$value."</td>
        </tr>";
}
echo "</table>";

?>

这将遍历您的奖杯并将其放入 HTML 表格中。

【讨论】:

    【解决方案2】:
    <?php
    $gamertag = 'x0--ii';
    $jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
    $json = file_get_contents($jsonurl);
    #var_dump(json_decode($json, true));
    
    /* json formatted for readability
    object(stdClass)#1 (2) { 
        ["status"]=> string(3) "200" 
        ["response"]=> object(stdClass)#2 (5) { // response is an array()
            ["onlineId"]=> string(6) "x0--II" 
            ["avatar"]=> string(77) "http://static-resource.np.community.playstation.net/avatar_m/SCEI/I0053_m.png" 
            ["plus"]=> int(0) 
            ["aboutMe"]=> string(0) "" 
            ["trophies"]=> object(stdClass)#3 (6) { // trophies is an array()
                ["trophyLevel"]=> int(3) 
                ["progress"]=> int(7) 
                ["platinum"]=> int(0) 
                ["gold"]=> int(2) 
                ["silver"]=> int(6) 
                ["bronze"]=> int(19) 
            } 
    
        }
    
    }
    */
    
    $json = json_decode($json, true);
    $rsp = $json['response']; // response array
    $trophies = $rsp['trophies']; // trophy array
    
    // examples
    $avatar = $rsp['avatar'];
    $onlineId = $rsp['onlineId'];
    $trophylevel = $trophies['trophyLevel'];
    
    // inline html example
    echo("<div><img src=\"".$rsp['avatar']."\" /></div>");
    // or
    echo("<div><img src=\"".$json['response']['avatar']."\" /></div>");
    
    echo("<div>".$json['trophies']['progress']."</div>");
    // or
    echo("<div>".$json['response']['trophies']['progress']."</div>");
    ?>
    

    【讨论】:

    • $trophies = $rsp['trophies'];是一个数组。使用 $trophies['progress'] , $trophies['platinum'] 等。我会更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2017-01-18
    • 2017-05-08
    • 1970-01-01
    • 2013-08-10
    相关资源
    最近更新 更多