正在获取 JSON 结果


Fetching JSON result

我在这里需要做的是从文件中获取info.php结果。

这是信息.php:

<?php
$dbh = new PDO('mysql:host=localhost;dbname=csgo', 'root', '');
$sth = $dbh->query("SELECT * FROM users");
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
    foreach($row as $r){
    $steamids = $r['steam_id'];
    $APIKEY = '********';
    $steamAPI = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=$steamids&key=$APIKEY&format=json";
    $json_object= file_get_contents($steamAPI);
    echo $json_object;
}

这是应用程序.js

$(document).ready(function(){
    $.getJSON("/info.php", function(json){
       $("#userinfo").html('<img style="width: 100px; height: 100px" style="width: 100px; height: 100px" src="'+json.response.players[0].avatarfull+'">');
       $("#steamProfile").html('<a href="'+json.response.players[0].profileurl+'">STEAM nuoroda</a>');
       $("#personaname").html(json.response.players[0].personaname);
    });
});

我应该如何在我的应用中正确提取.js ?

更新

$(document).ready(function(){
    $.getJSON("/info.php", function(json){
        $.each(json.response, function(i, player){
       $("#userinfo").html('<img style="width: 100px; height: 100px" style="width: 100px; height: 100px" src="'+player.players.avatarfull+'">');
       /*$("#steamProfile").html('<a href="'+json.response.players[0].profileurl+'">STEAM nuoroda</a>');
       $("#personaname").html(json.response.players[0].personaname);*/
       });
    });
});

现在我明白了Uncaught TypeError: Cannot read property 'length' of undefined

你的 PHP 文件将一个接一个地输出几个不相关的 JSON blob,例如:

{..}{..}{..}

这使得 JSON 解析器无法解析它。您必须解码数据并构建一个新的有效 JSON 结构:

$sth = $dbh->query("SELECT * FROM users");
$data = [];
foreach ($sth as $r) { // no need to `fetchAll`
    ...
    $json = file_get_contents($steamAPI);
    $data[] = json_decode($json);
}
echo json_encode($data);