我試圖從這個 API 檔案中決議一個物件中的一個物件。我能夠決議主要物件,允許列印我需要的資料。我對我應該如何在特定物件中列印感到困惑。例如,我的 loadPlayers() 函式,我試圖檢索特定玩家所在的團隊。我試圖參考實際元素(即“full_name”),但我相信我沒有正確訪問子陣列。任何資訊將不勝感激!鏈接到用于播放器功能的 api 檔案:https ://www.balldontlie.io/#players
//Loads Player Data
function loadPlayers() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var players = JSON.parse(this.responseText);
// get 'data' key inside response
var playersData = players.data;
// loop all the players
for (var player of playersData) {
document.getElementById("players").innerHTML = "<br />" player["first_name"] " " player["last_name"] ", " player["position"] ", " player["full_name"];
}
}
};
xhttp.open("GET", "https://www.balldontlie.io/api/v1/players", true);
xhttp.send();
}
//Loads Game Data
function loadGames() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var games = JSON.parse(this.responseText);
// get 'data' key inside response
var gamesData = games.data;
// loop all the games
for (var game of gamesData) {
//print score data
document.getElementById("games").innerHTML = "<br />" game["home_team_score"] " - " game["visitor_team_score"];
}
}
};
xhttp.open("GET", "https://www.balldontlie.io/api/v1/games", true);
xhttp.send();
}
//Loads Team Data
function loadTeams() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var teams = JSON.parse(this.responseText);
// get 'data' key inside response
var teamsData = teams.data;
// loop all the teams
for (var team of teamsData) {
// print full name and abbreivation
document.getElementById("games").innerHTML = "<br />" team["full_name"] ", " team["abbreviation"] ", " team["conference"] ;
}
}
};
xhttp.open("GET", "https://www.balldontlie.io/api/v1/teams", true);
xhttp.send();
}
<!DOCTYPE html>
<html>
<body style="background-color:peachpuff;" >
<center>NBA STATS</center>
<center><marquee behavior="scroll" direction="right" scrollamount="12.5">Data Extracted From BDL API</marquee></center>
<center> <a href="https://www.balldontlie.io/#introduction">View API Docs</a> </center>
<script src="main.js"></script>
<div id="players">
<button type="button" onclick="loadPlayers()">View Players</button>
</div>
<div id = "teams">
<button type="button2" onclick="loadTeams()">View Teams</button>
</div>
<div id ="games">
<button type="button3" onclick="loadGames()">View Games</button>
<div>
</body>
</html>
uj5u.com熱心網友回復:
loadPlayers在你的函式中試試這個:
for (var player of playersData) {
document.getElementById("players").innerHTML = "<br />" player["first_name"] " " player["last_name"] ", " player["position"] ", " player["full_name"] " " player.team.full_name;
}
每個玩家內部的team屬性都是一個物件,所以你必須像這樣參考:player.team.ATTRIBUTE_INSIDE_TEAM_OBJECT
uj5u.com熱心網友回復:
我可以從回應中看到您嘗試獲取的“full_name”在團隊物件中。為了得到它,你寫player["team"]["full_name"]
uj5u.com熱心網友回復:
要獲得團隊,您可以嘗試:players.data[0].team.full_name
或在您的回圈中: player.team.full_name
直到您習慣了腦海中的樹形視圖,請嘗試使用在線 json 美化器:https ://jsonbeautifier.org/
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/475826.html
標籤:javascript html json api 后端
