Javascript 匹配JSON对象中的键/值
我有一个学校项目,我们正在学习JSON。我试图做的是找出如何将键与另一个对象属性中存在的其他键进行匹配 我正在使用一个旧的api来获取nfl球员信息。以下是用于提取数据的url示例: 我使用AJAX调用数据并将结果字符串化到一个表中Javascript 匹配JSON对象中的键/值,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我有一个学校项目,我们正在学习JSON。我试图做的是找出如何将键与另一个对象属性中存在的其他键进行匹配 我正在使用一个旧的api来获取nfl球员信息。以下是用于提取数据的url示例: 我使用AJAX调用数据并将结果字符串化到一个表中 $.ajax({ url: queryURL, method: "GET" }).then(function(response) { var tbl = $("<table>"); $(tbl).addClas
$.ajax({
url: queryURL,
method: "GET"
}).then(function(response) {
var tbl = $("<table>");
$(tbl).addClass("table");
var objCount = JSON.stringify(response.players.length);
$(tbl).append("<thead><tr><th>ID</th><th>Team</th><th>POS</th>
<th>Player</th><th>Stat</th></tr></thead><tbody>");
for (p = 1; p < 2; p++) {
var id = response.players[p].id;
var team = response.players[p].teamAbbr;
var pos = response.players[p].position;
var plyr = response.players[p].name;
var stat = JSON.stringify(response.players[p].stats);
var plyrStatsObjLen =
JSON.stringify(response.players[p].stats.length);
console.log("statObjLength: " + plyrStatsObjLen);
$.each(response.players[p].stats, function(key, value) {
console.log(key + ": " + value);
});
$(tbl).append("<tr><td>" + id + "</td><td>" + team + "</td><td>" + pos + "</td><td>" + plyr + "</td><td>" + stat + "</td>");
}
$(tbl).append("</tbody><br/><br/>");
$("#statOutput").append(tbl);
});
例如,key ID“1”对应于stat引用对象中的“Games Played”
我对所有这些都是新手,所以我不能理解的是,如果我想用stats引用对象中相应的name值来细分输出中的键,我该怎么做
例如,从JSFIDLE输出,而不是
{"1":"9","13":"1"}
它会说
Games Played: 9, Rushing Attempts: 1
我希望这是有道理的。基本上,我想学习如何将一个JSON对象中的键和另一个JSON对象中的键值相匹配
非常感谢您的帮助 您可以将第二个AJAX调用嵌套在第一个调用的success函数中,然后将变量分配和表创建放入第二个success函数中。在第二个成功函数中,您可以使用简单的
for
循环将玩家数据中的每个数字统计与统计数据中统计的正确名称相匹配,如下所示:
$(document).ready(function () {
var statType = "seasonStats";
var season = "2018";
var week = "15";
var playersURL = "https://api.fantasy.nfl.com/v1/players/stats?format=json" + "&statType=" + statType + "&season=" + season + "&week=" + week;
var statURL = "https://api.fantasy.nfl.com/v1/game/stats?format=json";
// Now we get the stats
$.ajax({
url: statURL,
method: "GET",
success: function (response) {
const stats = response.stats;
// Then we get the players
$.ajax({
url: playersURL,
method: "GET",
success: function (response) {
const players = response.players;
// Now we do the rest of the logic
// Here's our table creation and header
var tbl = $("<table>");
$(tbl).addClass("table");
$(tbl).append("<thead><tr><th>ID</th><th>Team</th><th>POS</th><th>Player</th><th>Stat</th></tr></thead><tbody>");
// Here's where we create variables for each individual player
for (p = 0; p < 1; p++) {
let id = players[p].id;
let team = players[p].teamAbbr;
let pos = players[p].position;
let plyr = players[p].name;
// We create an empty object to hold the named statistics we're about to find
let statistics = {};
// Now we'll loop over the players and statistics to get names for all the stats
playerStats = players[p].stats;
for (playerStat in playerStats) {
for (s = 0; s < stats.length; s++) {
// if the player's statistic matches the id of the property from the stats object, we add that stat name and its total for that player as a property of the object we created above
if (playerStat === JSON.stringify(stats[s].id)) {
let statName = stats[s].name;
let statCount = playerStats[playerStat];
statistics[statName] = statCount;
}
}
};
// Now we turn our statistics object into text that can actually go into our table
let prettyStats = "";
for (statistic in statistics) {
prettyStats = prettyStats + `${statistic}: ${statistics[statistic]}
`
}
// Now that we have data for the player, we add a row to our table
$(tbl).append("<tr><td>" + id + "</td><td>" + team + "</td><td>" + pos + "</td><td>" + plyr + "</td><td>" + prettyStats + "</td>");
}
//Here's the bottom of our table and its creation inside the div
$(tbl).append("</tbody><br/><br/>");
$("#statOutput").append(tbl);
}
});
}
});
});
$(文档).ready(函数(){
var statType=“季节统计”;
var season=“2018”;
var week=“15”;
var playersURL=”https://api.fantasy.nfl.com/v1/players/stats?format=json“+”&statType=“+statType+”&season=“+season+”&week=“+week;
变量statURL=”https://api.fantasy.nfl.com/v1/game/stats?format=json";
//现在我们得到数据了
$.ajax({
网址:statURL,
方法:“获取”,
成功:功能(响应){
const stats=response.stats;
//然后我们得到了球员
$.ajax({
url:PlayerURL,
方法:“获取”,
成功:功能(响应){
const players=response.players;
//现在我们做剩下的逻辑
//这是我们的表创建和标题
变量tbl=$(“”);
$(待定).addClass(“表”);
$(待定).append(“IDTeamPOSPlayerStat”);
//这里我们为每个玩家创建变量
对于(p=0;p<1;p++){
设id=players[p].id;
让团队=玩家[p].teamAbbr;
设pos=players[p]。位置;
让plyr=players[p].name;
//我们创建一个空对象来保存我们将要查找的命名统计信息
设statistics={};
//现在我们将循环查看球员和统计数据,以获得所有统计数据的名称
playerStats=玩家[p].stats;
for(playerStats中的playerStat){
对于(s=0;s
”);
$(“#statOutput”)。追加(待定);
}
});
}
});
});
您可能希望对
prettyStats
的输出进行进一步的文本格式化,但它可以为您获取所需的数据。这真是太棒了。很好用!我曾考虑过如何循环统计数据和匹配数据,但无法将其概念化,因为对这一切都是陌生的。非常感谢!我欠你一个人情,我的荣幸!祝项目的其余部分好运。
$(document).ready(function () {
var statType = "seasonStats";
var season = "2018";
var week = "15";
var playersURL = "https://api.fantasy.nfl.com/v1/players/stats?format=json" + "&statType=" + statType + "&season=" + season + "&week=" + week;
var statURL = "https://api.fantasy.nfl.com/v1/game/stats?format=json";
// Now we get the stats
$.ajax({
url: statURL,
method: "GET",
success: function (response) {
const stats = response.stats;
// Then we get the players
$.ajax({
url: playersURL,
method: "GET",
success: function (response) {
const players = response.players;
// Now we do the rest of the logic
// Here's our table creation and header
var tbl = $("<table>");
$(tbl).addClass("table");
$(tbl).append("<thead><tr><th>ID</th><th>Team</th><th>POS</th><th>Player</th><th>Stat</th></tr></thead><tbody>");
// Here's where we create variables for each individual player
for (p = 0; p < 1; p++) {
let id = players[p].id;
let team = players[p].teamAbbr;
let pos = players[p].position;
let plyr = players[p].name;
// We create an empty object to hold the named statistics we're about to find
let statistics = {};
// Now we'll loop over the players and statistics to get names for all the stats
playerStats = players[p].stats;
for (playerStat in playerStats) {
for (s = 0; s < stats.length; s++) {
// if the player's statistic matches the id of the property from the stats object, we add that stat name and its total for that player as a property of the object we created above
if (playerStat === JSON.stringify(stats[s].id)) {
let statName = stats[s].name;
let statCount = playerStats[playerStat];
statistics[statName] = statCount;
}
}
};
// Now we turn our statistics object into text that can actually go into our table
let prettyStats = "";
for (statistic in statistics) {
prettyStats = prettyStats + `${statistic}: ${statistics[statistic]}
`
}
// Now that we have data for the player, we add a row to our table
$(tbl).append("<tr><td>" + id + "</td><td>" + team + "</td><td>" + pos + "</td><td>" + plyr + "</td><td>" + prettyStats + "</td>");
}
//Here's the bottom of our table and its creation inside the div
$(tbl).append("</tbody><br/><br/>");
$("#statOutput").append(tbl);
}
});
}
});
});