2016-08-26 6 views
1

Okay, das ist meine erste Veröffentlichung auf dieser Website. Ich brauche ein wenig Hilfe, ich versuche, Daten von einer Json API in eine HTML-Tabelle hinzuzufügen. Das habe ich bisher.Wie liest man eine JSON-Datei in HTML-Tabelle PHP?

<?php 

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

<? 

Hier ist meine Ausgabe:

object(stdClass)#1 (2) { ["status"]=> string(3) "200" ["response"]=> object(stdClass)#2 (5) { ["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) { ["trophyLevel"]=> int(3) ["progress"]=> int(7) ["platinum"]=> int(0) ["gold"]=> int(2) ["silver"]=> int(6) ["bronze"]=> int(19) } } }

ich das Bild sowie alle Werte angezeigt werden sollen. Danke für jede Hilfe!

+3

Sobald Sie den JSON dekodieren, ist es eine Datenstruktur wie jede andere in PHP. Sie greifen auf Daten in der Struktur genau wie jede andere Struktur zu. –

+1

Yup, benutze 'json_decode ($ json, true)', wenn du darauf wie ein Array zugreifen willst. Andernfalls müssen Sie '->' für Eigenschaften verwenden. Die Bilder, die Sie gerade um '' Tags wickeln. –

Antwort

0
<?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>"); 
?> 
+0

Es hat funktioniert! Danke – Killer360Hacks

+0

$ Trophäen = $ rsp ['Trophäen']; ist ein Array. benutze $ trophies ['progress'], $ trophies ['platinum'] usw. Ich werde meine Antwort aktualisieren. – Brian

0

Sie würden die json_decode Daten in eine PHP-Variable gesetzt, und es Zugang wie ein normales PHP Objektarray

Hier ist ein kurzes Beispiel:

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

?> 

Das würde Schleife durch Ihre Trophäen setzen es in eine HTML-Tabelle.