如果 JSON 中的数据存在语句不起作用
Posted
技术标签:
【中文标题】如果 JSON 中的数据存在语句不起作用【英文标题】:if data exists statement in JSON not working 【发布时间】:2021-01-31 11:03:40 【问题描述】:我有一个 JSON sn-p,它应该检测 API 中是否存在特定类型的数据。
api端点是https://fortnite-api.com/v1/stats/br/v2?name=chefbezos
当我运行console.log(data.data.stats.all.trio)时,它给了我null。但它仍然显示带有 triostats 类的 div。代码有问题吗,因为data.data.stats.all.trio == "null"
为真,因为console.log(data.data.stats.all.trio)
为空。感谢您的帮助!
if(typeof data.data.stats.all.trio == "null")
var trioAllMP = data.data.stats.all.trio.minutesPlayed;
var trioAllMatches = data.data.stats.all.trio.matches;
var trioML = Math.round((trioAllMP/trioAllMatches + Number.EPSILON) * 100) / 100 + ' Minutes';
var trioAllWins = data.data.stats.all.trio.wins;
var trioAllWinrate = data.data.stats.all.trio.winRate;
var trioAllKills = data.data.stats.all.trio.kills;
var trioAllDeaths = data.data.stats.all.trio.deaths;
var trioAllKd = data.data.stats.all.trio.kd;
var trioAllKPM = data.data.stats.all.trio.killsPerMin;
var trioAllScore = data.data.stats.all.trio.score;
var trioAllSPM = data.data.stats.all.trio.scorePerMin;
var trioAllSPM2 = data.data.stats.all.trio.scorePerMatch;
var trioAllPlacetop3 = data.data.stats.all.trio.top3;
var trioAllPlacetop6 = data.data.stats.all.trio.top6;
var trioAllPO = data.data.stats.all.trio.playersOutlived;
var trioTime = time_convert(trioAllMP);
document.getElementById('trioAllMP').innerhtml = '<b>Time Played:</b> ' + trioTime;
document.getElementById('trioAllScore').innerHTML = '<b>Trio Score:</b> ' + trioAllScore;
document.getElementById('trioAllMatches').innerHTML = '<b>Number of Matches:</b> '+ trioAllMatches;
document.getElementById('trioMatchLength').innerHTML ='<b>AVG Length of Match:</b> '+ trioML;
document.getElementById('trioAllWins').innerHTML = '<b>Total Wins:</b> ' + trioAllWins;
document.getElementById('trioAllWinrate').innerHTML = '<b>Trio Winrate:</b> ' + trioAllWinrate;
document.getElementById('trioAllKills').innerHTML = '<b>Trio Eliminations:</b> ' + trioAllKills;
document.getElementById('trioAllDeaths').innerHTML = '<b>Trio Deaths:</b> ' + trioAllDeaths;
document.getElementById('trioAllKd').innerHTML = '<b>Trio K/D Rate:</b> ' + trioAllKd;
document.getElementById('trioAllKPM').innerHTML = '<b>Eliminations Per Minute:</b> ' + trioAllKPM;
document.getElementById('trioAllSPM').innerHTML ='<b>Trio Score Per Minute:</b> ' + trioAllSPM;
document.getElementById('trioAllSPM2').innerHTML ='<b>Trio Score Per Match:</b> ' + trioAllSPM2;
document.getElementById('trioAllPlacetop3').innerHTML ='<b>Placed Top 3:</b> '+ trioAllPlacetop3;
document.getElementById('trioAllPlacetop6').innerHTML='<b>Placed top 6:</b> '+ trioAllPlacetop6;
document.getElementById('trioAllPO').innerHTML = '<b>Players Outlasted:</b> ' + trioAllPO;
if(typeof data.data.stats.all.trio == "null")
$(".trioStats").remove;
所有代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!--<link href="https://fonts.googleapis.com/css2?family=Raleway:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,8
00;1,900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Commissioner:wght@100;200;300;400;500;600;700;800;900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Nunito:ital,wght@0,200;0,300;0,400;0,600;0,700;0,800;0,900;1,200;1,300;1,400;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Work+Sans:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Barlow:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Oxygen:wght@300;400;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap" rel="stylesheet">-->
<link
href="https://fonts.googleapis.com/css?family=Google+Sans+Display:wght@300;400;700&display=swap"
rel="stylesheet"
/>
<!-- Compiled and minified CSS -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"
/>
<!-- Compiled and minified javascript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<!--
fonts:
font-family: 'Nunito', sans-serif;
font-family: 'Work Sans', sans-serif;
font-family: 'Barlow', sans-serif;
font-family: 'Josefin Sans', sans-serif;
font-family: 'Inter', sans-serif;
font-family: 'Oxygen', sans-serif;
font-family: 'Hind Siliguri', sans-serif;
font-family: 'Lato', sans-serif;
font-family: 'Poppins', sans-serif;
font-family: 'Josefin Sans', sans-serif;
-->
</head>
<style>
@font-face
font-family: BurbankBigCondensed-Black;
src: url('Burbank Big Condensed Black.ttf') format('opentype');
font-weight: 200;
body
line-height: 0.7em;
font-family: 'Google Sans Display', sans-serif;
.box
display: flex;
justify-content: space-evenly;
.hide
display:none;
</style>
<body>
<form id="form" onsubmit="return false;">
<input type="text" id="myText" value="chefbezos" />
<button onclick="myFunction()">Try it</button>
</form>
<h2 id="name"></h2>
<h2 id="userId"></h2>
<div class="col s3 overallStats">
<h6 class="specialTag" id="overallAllMP"></h6>
<h6 class="specialTag" id="overallAllWins"></h6>
<h6 class="specialTag" id="overallAllMatches"></h6>
<h6 class="specialTag" id="overallAllScore"></h6>
<h6 class="specialTag" id="overallMatchLength"></h6>
<h6 class="specialTag" id="overallAllWinrate"></h6>
<h6 class="specialTag" id="overallAllKills"></h6>
<h6 class="specialTag" id="overallAllDeaths"></h6>
<h6 id="overallAllKd"></h6>
<h6 id="overallAllKPM"></h6>
<h6 id="overallAllSPM"></h6>
<h6 id="overallAllSPM2"></h6>
<h6 id="overallAllPlacetop3"></h6>
<h6 id="overallAllPlacetop5"></h6>
<h6 id="overallAllPlacetop6"></h6>
<h6 id="overallAllPlacetop10"></h6>
<h6 id="overallAllPlacetop12"></h6>
<h6 id="overallAllPlacetop25"></h6>
<h6 id="overallAllPO"></h6>
</div>
<div class="row">
<div class="col s3 soloStats">
<h2>Solo Stats</h2>
<h6 class="specialTag" id="soloAllMP"></h6>
<h6 class="specialTag" id="soloAllMatches"></h6>
<h6 class="specialTag" id="soloMatchLength"></h6>
<h6 class="specialTag" id="soloAllWins"></h6>
<h6 class="specialTag" id="soloAllWinrate"></h6>
<h6 class="specialTag" id="soloAllKills"></h6>
<h6 class="specialTag" id="soloAllDeaths"></h6>
<h6 id="soloAllKd"></h6>
<h6 id="soloAllKPM"></h6>
<h6 id="soloAllScore"></h6>
<h6 id="soloAllSPM"></h6>
<h6 id="soloAllSPM2"></h6>
<h6 id="soloAllPlacetop10"></h6>
<h6 id="soloAllPlacetop25"></h6>
<h6 id="soloAllPO"></h6>
</div>
<div class="col s3 duoStats">
<h2>Duo Stats</h2>
<h6 class="specialTag" id="duoAllMP"></h6>
<h6 class="specialTag" id="duoAllMatches"></h6>
<h6 class="specialTag" id="duoMatchLength"></h6>
<h6 class="specialTag" id="duoAllWins"></h6>
<h6 class="specialTag" id="duoAllWinrate"></h6>
<h6 class="specialTag" id="duoAllKills"></h6>
<h6 class="specialTag" id="duoAllDeaths"></h6>
<h6 id="duoAllKd"></h6>
<h6 id="duoAllKPM"></h6>
<h6 id="duoAllScore"></h6>
<h6 id="duoAllSPM"></h6>
<h6 id="duoAllSPM2"></h6>
<h6 id="duoAllPlacetop5"></h6>
<h6 id="duoAllPlacetop12"></h6>
<h6 id="duoAllPO"></h6>
</div>
<div class="col s3 trioStats">
<h2>Trio Stats</h2>
<h6 class="specialTag" id="trioAllMP"></h6>
<h6 class="specialTag" id="trioAllMatches"></h6>
<h6 class="specialTag" id="trioMatchLength"></h6>
<h6 class="specialTag" id="trioAllWins"></h6>
<h6 class="specialTag" id="trioAllWinrate"></h6>
<h6 class="specialTag" id="trioAllKills"></h6>
<h6 class="specialTag" id="trioAllDeaths"></h6>
<h6 id="trioAllKd"></h6>
<h6 id="trioAllKPM"></h6>
<h6 id="trioAllScore"></h6>
<h6 id="trioAllSPM"></h6>
<h6 id="trioAllSPM2"></h6>
<h6 id="trioAllPlacetop3"></h6>
<h6 id="trioAllPlacetop6"></h6>
<h6 id="trioAllPO"></h6>
</div>
<div class="col s3 squadStats">
<h2>Squad Stats</h2>
<h6 class="specialTag" id="squadAllMP"></h6>
<h6 class="specialTag" id="squadAllMatches"></h6>
<h6 class="specialTag" id="squadMatchLength"></h6>
<h6 class="specialTag" id="squadAllWins"></h6>
<h6 class="specialTag" id="squadAllWinrate"></h6>
<h6 class="specialTag" id="squadAllKills"></h6>
<h6 class="specialTag" id="squadAllDeaths"></h6>
<h6 id="squadAllKd"></h6>
<h6 id="squadAllKPM"></h6>
<h6 id="squadAllScore"></h6>
<h6 id="squadAllSPM"></h6>
<h6 id="squadAllSPM2"></h6>
<h6 id="squadAllPlacetop3"></h6>
<h6 id="squadAllPlacetop6"></h6>
<h6 id="squadAllPO"></h6>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<script>
function myFunction()
var username = document.getElementById('myText').value;
$.getJSON(
url: 'https://fortnite-api.com/v1/stats/br/v2?name=' + username + '',
success: function (data)
if(typeof data.data.stats.all.trio == "null")
$(".trioStats").addClass("hide");
console.log( data.data.stats.all.trio);
if(typeof data.data.stats.all.trio == "null")
var trioAllMP = data.data.stats.all.trio.minutesPlayed;
var trioAllMatches = data.data.stats.all.trio.matches;
var trioML = Math.round((trioAllMP/trioAllMatches + Number.EPSILON) * 100) / 100 + ' Minutes';
var trioAllWins = data.data.stats.all.trio.wins;
var trioAllWinrate = data.data.stats.all.trio.winRate;
var trioAllKills = data.data.stats.all.trio.kills;
var trioAllDeaths = data.data.stats.all.trio.deaths;
var trioAllKd = data.data.stats.all.trio.kd;
var trioAllKPM = data.data.stats.all.trio.killsPerMin;
var trioAllScore = data.data.stats.all.trio.score;
var trioAllSPM = data.data.stats.all.trio.scorePerMin;
var trioAllSPM2 = data.data.stats.all.trio.scorePerMatch;
var trioAllPlacetop3 = data.data.stats.all.trio.top3;
var trioAllPlacetop6 = data.data.stats.all.trio.top6;
var trioAllPO = data.data.stats.all.trio.playersOutlived;
var trioTime = time_convert(trioAllMP);
document.getElementById('trioAllMP').innerHTML = '<b>Time Played:</b> ' + trioTime;
document.getElementById('trioAllScore').innerHTML = '<b>Trio Score:</b> ' + trioAllScore;
document.getElementById('trioAllMatches').innerHTML = '<b>Number of Matches:</b> '+ trioAllMatches;
document.getElementById('trioMatchLength').innerHTML ='<b>AVG Length of Match:</b> '+ trioML;
document.getElementById('trioAllWins').innerHTML = '<b>Total Wins:</b> ' + trioAllWins;
document.getElementById('trioAllWinrate').innerHTML = '<b>Trio Winrate:</b> ' + trioAllWinrate;
document.getElementById('trioAllKills').innerHTML = '<b>Trio Eliminations:</b> ' + trioAllKills;
document.getElementById('trioAllDeaths').innerHTML = '<b>Trio Deaths:</b> ' + trioAllDeaths;
document.getElementById('trioAllKd').innerHTML = '<b>Trio K/D Rate:</b> ' + trioAllKd;
document.getElementById('trioAllKPM').innerHTML = '<b>Eliminations Per Minute:</b> ' + trioAllKPM;
document.getElementById('trioAllSPM').innerHTML ='<b>Trio Score Per Minute:</b> ' + trioAllSPM;
document.getElementById('trioAllSPM2').innerHTML ='<b>Trio Score Per Match:</b> ' + trioAllSPM2;
document.getElementById('trioAllPlacetop3').innerHTML ='<b>Placed Top 3:</b> '+ trioAllPlacetop3;
document.getElementById('trioAllPlacetop6').innerHTML='<b>Placed top 6:</b> '+ trioAllPlacetop6;
document.getElementById('trioAllPO').innerHTML = '<b>Players Outlasted:</b> ' + trioAllPO;
else if(typeof data.data.stats.all.trio == "null")
$(".trioStats").addClass("hide");
function time_convert(num)
var hours = Math.floor(num / 60);
var minutes = num % 60;
return hours + ' Hr ' + minutes + ' Min';
var overallAllMP = data.data.stats.all.overall.minutesPlayed;
var overallAllMatches = data.data.stats.all.overall.matches;
var overallML = Math.round((overallAllMP/overallAllMatches + Number.EPSILON) * 100) / 100 + ' Minutes';
var overallAllWins = data.data.stats.all.overall.wins;
var overallAllWinrate = data.data.stats.all.overall.winRate;
var overallAllKills = data.data.stats.all.overall.kills;
var overallAllDeaths = data.data.stats.all.overall.deaths;
var overallAllKd = data.data.stats.all.overall.kd;
var overallAllKPM = data.data.stats.all.overall.killsPerMin;
var overallAllScore = data.data.stats.all.overall.score;
var overallAllSPM = data.data.stats.all.overall.scorePerMin;
var overallAllSPM2 = data.data.stats.all.overall.scorePerMatch;
var overallAllPlacetop3 = data.data.stats.all.overall.top3;
var overallAllPlacetop5 = data.data.stats.all.overall.top5;
var overallAllPlacetop6 = data.data.stats.all.overall.top6;
var overallAllPlacetop10 = data.data.stats.all.overall.top10;
var overallAllPlacetop12 = data.data.stats.all.overall.top12;
var overallAllPlacetop25 = data.data.stats.all.overall.top25;
var overallAllPO = data.data.stats.all.overall.playersOutlived;
var overallTime = time_convert(overallAllMP);
document.getElementById('overallAllMP').innerHTML = '<b>Time Played:</b> ' + overallTime;
document.getElementById('overallAllScore').innerHTML = '<b>Overall Score:</b> ' + overallAllScore;
document.getElementById('overallAllMatches').innerHTML = '<b>Number of Matches:</b> '+ overallAllMatches;
document.getElementById('overallMatchLength').innerHTML ='<b>AVG Length of Match:</b> '+ overallML;
document.getElementById('overallAllWins').innerHTML = '<b>Total Wins:</b> ' + overallAllWins;
document.getElementById('overallAllWinrate').innerHTML = '<b>Overall Winrate:</b> ' + overallAllWinrate + '%';
document.getElementById('overallAllKills').innerHTML = '<b>Overall Eliminations:</b> ' + overallAllKills;
document.getElementById('overallAllDeaths').innerHTML = '<b>Overall Deaths:</b> ' + overallAllDeaths;
document.getElementById('overallAllKd').innerHTML = '<b>Overall K/D Rate:</b> ' + overallAllKd;
document.getElementById('overallAllKPM').innerHTML = '<b>Eliminations Per Minute:</b> ' + overallAllKPM;
document.getElementById('overallAllSPM').innerHTML ='<b>Overall Score Per Minute:</b> ' + overallAllSPM;
document.getElementById('overallAllSPM2').innerHTML ='<b>Overall Score Per Match:</b> ' + overallAllSPM2;
document.getElementById('overallAllPlacetop3').innerHTML = '<b>Placed Top 3:</b> ' + overallAllPlacetop3;
document.getElementById('overallAllPlacetop5').innerHTML = '<b>Placed top 5:</b> ' + overallAllPlacetop5;
document.getElementById('overallAllPlacetop6').innerHTML = '<b>Placed top 6:</b> ' + overallAllPlacetop6;
document.getElementById('overallAllPlacetop10').innerHTML ='<b>Placed Top 10:</b> '+ overallAllPlacetop5;
document.getElementById('overallAllPlacetop12').innerHTML='<b>Placed top 12:</b> '+ overallAllPlacetop12;
document.getElementById('overallAllPlacetop25').innerHTML='<b>Placed top 25:</b> '+ overallAllPlacetop25;
document.getElementById('overallAllPO').innerHTML = '<b>Players Outlasted:</b> ' + overallAllPO;
var duoAllMP = data.data.stats.all.duo.minutesPlayed;
var duoAllMatches = data.data.stats.all.duo.matches;
var duoML = Math.round((duoAllMP/duoAllMatches + Number.EPSILON) * 100) / 100 + ' Minutes';
var duoAllWins = data.data.stats.all.duo.wins;
var duoAllWinrate = data.data.stats.all.duo.winRate;
var duoAllKills = data.data.stats.all.duo.kills;
var duoAllDeaths = data.data.stats.all.duo.deaths;
var duoAllKd = data.data.stats.all.duo.kd;
var duoAllKPM = data.data.stats.all.duo.killsPerMin;
var duoAllScore = data.data.stats.all.duo.score;
var duoAllSPM = data.data.stats.all.duo.scorePerMin;
var duoAllSPM2 = data.data.stats.all.duo.scorePerMatch;
var duoAllPlacetop5 = data.data.stats.all.duo.top5;
var duoAllPlacetop12 = data.data.stats.all.duo.top12;
var duoAllPO = data.data.stats.all.duo.playersOutlived;
var duoTime = time_convert(duoAllMP);
document.getElementById('duoAllMP').innerHTML = '<b>Time Played:</b> ' + duoTime;
document.getElementById('duoAllScore').innerHTML = '<b>duo Score:</b> ' + duoAllScore;
document.getElementById('duoAllMatches').innerHTML = '<b>Number of Matches:</b> '+ duoAllMatches;
document.getElementById('duoMatchLength').innerHTML ='<b>AVG Length of Match:</b> '+ duoML;
document.getElementById('duoAllWins').innerHTML = '<b>Total Wins:</b> ' + duoAllWins;
document.getElementById('duoAllWinrate').innerHTML = '<b>Duo Winrate:</b> ' + duoAllWinrate + '%';
document.getElementById('duoAllKills').innerHTML = '<b>Duo Eliminations:</b> ' + duoAllKills;
document.getElementById('duoAllDeaths').innerHTML = '<b>Duo Deaths:</b> ' + duoAllDeaths;
document.getElementById('duoAllKd').innerHTML = '<b>Duo K/D Rate:</b> ' + duoAllKd;
document.getElementById('duoAllKPM').innerHTML = '<b>Eliminations Per Minute:</b> ' + duoAllKPM;
document.getElementById('duoAllSPM').innerHTML ='<b>Duo Score Per Minute:</b> ' + duoAllSPM;
document.getElementById('duoAllSPM2').innerHTML ='<b>Duo Score Per Match:</b> ' + duoAllSPM2;
document.getElementById('duoAllPlacetop5').innerHTML = '<b>Placed top 5:</b> ' + duoAllPlacetop5;
document.getElementById('duoAllPlacetop12').innerHTML='<b>Placed top 12:</b> '+ duoAllPlacetop12;
document.getElementById('duoAllPO').innerHTML = '<b>Players Outlasted:</b> ' + duoAllPO;
var soloAllMP = data.data.stats.all.solo.minutesPlayed;
var soloAllMatches = data.data.stats.all.solo.matches;
var soloML = Math.round((soloAllMP/soloAllMatches + Number.EPSILON) * 100) / 100 + ' Minutes';
var soloAllWins = data.data.stats.all.solo.wins;
var soloAllWinrate = data.data.stats.all.solo.winRate;
var soloAllKills = data.data.stats.all.solo.kills;
var soloAllDeaths = data.data.stats.all.solo.deaths;
var soloAllKd = data.data.stats.all.solo.kd;
var soloAllKPM = data.data.stats.all.solo.killsPerMin;
var soloAllScore = data.data.stats.all.solo.score;
var soloAllSPM = data.data.stats.all.solo.scorePerMin;
var soloAllSPM2 = data.data.stats.all.solo.scorePerMatch;
var soloAllPlacetop10 = data.data.stats.all.solo.top10;
var soloAllPlacetop25 = data.data.stats.all.solo.top25;
var soloAllPO = data.data.stats.all.solo.playersOutlived;
var soloTime = time_convert(soloAllMP);
document.getElementById('soloAllMP').innerHTML = '<b>Time Played:</b> ' + soloTime;
document.getElementById('soloAllScore').innerHTML = '<b>solo Score:</b> ' + soloAllScore;
document.getElementById('soloAllMatches').innerHTML = '<b>Number of Matches:</b> '+ soloAllMatches;
document.getElementById('soloMatchLength').innerHTML ='<b>AVG Length of Match:</b> '+ soloML;
document.getElementById('soloAllWins').innerHTML = '<b>Total Wins:</b> ' + soloAllWins;
document.getElementById('soloAllWinrate').innerHTML = '<b>Solo Winrate:</b> ' + soloAllWinrate + '%';
document.getElementById('soloAllKills').innerHTML = '<b>Solo Eliminations:</b> ' + soloAllKills;
document.getElementById('soloAllDeaths').innerHTML = '<b>Solo Deaths:</b> ' + soloAllDeaths;
document.getElementById('soloAllKd').innerHTML = '<b>Solo K/D Rate:</b> ' + soloAllKd;
document.getElementById('soloAllKPM').innerHTML = '<b>Eliminations Per Minute:</b> ' + soloAllKPM;
document.getElementById('soloAllSPM').innerHTML ='<b>Solo Score Per Minute:</b> ' + soloAllSPM;
document.getElementById('soloAllSPM2').innerHTML ='<b>Solo Score Per Match:</b> ' + soloAllSPM2;
document.getElementById('soloAllPlacetop10').innerHTML ='<b>Placed Top 10:</b> '+ soloAllPlacetop10;
document.getElementById('soloAllPlacetop25').innerHTML='<b>Placed top 25:</b> '+ soloAllPlacetop25;
document.getElementById('soloAllPO').innerHTML = '<b>Players Outlasted:</b> ' + soloAllPO;
var squadAllMP = data.data.stats.all.squad.minutesPlayed;
var squadAllMatches = data.data.stats.all.squad.matches;
var squadML = Math.round((squadAllMP/squadAllMatches + Number.EPSILON) * 100) / 100 + ' Minutes';
var squadAllWins = data.data.stats.all.squad.wins;
var squadAllWinrate = data.data.stats.all.squad.winRate;
var squadAllKills = data.data.stats.all.squad.kills;
var squadAllDeaths = data.data.stats.all.squad.deaths;
var squadAllKd = data.data.stats.all.squad.kd;
var squadAllKPM = data.data.stats.all.squad.killsPerMin;
var squadAllScore = data.data.stats.all.squad.score;
var squadAllSPM = data.data.stats.all.squad.scorePerMin;
var squadAllSPM2 = data.data.stats.all.squad.scorePerMatch;
var squadAllPlacetop3 = data.data.stats.all.squad.top3;
var squadAllPlacetop6 = data.data.stats.all.squad.top6;
var squadAllPO = data.data.stats.all.squad.playersOutlived;
var squadTime = time_convert(squadAllMP);
document.getElementById('squadAllMP').innerHTML = '<b>Time Played:</b> ' + squadTime;
document.getElementById('squadAllScore').innerHTML = '<b>Squad Score:</b> ' + squadAllScore;
document.getElementById('squadAllMatches').innerHTML = '<b>Number of Matches:</b> '+ squadAllMatches;
document.getElementById('squadMatchLength').innerHTML ='<b>AVG Length of Match:</b> '+ squadML;
document.getElementById('squadAllWins').innerHTML = '<b>Total Wins:</b> ' + squadAllWins;
document.getElementById('squadAllWinrate').innerHTML = '<b>Squad Winrate:</b> ' + squadAllWinrate + '%';
document.getElementById('squadAllKills').innerHTML = '<b>Squad Eliminations:</b> ' + squadAllKills;
document.getElementById('squadAllDeaths').innerHTML = '<b>Squad Deaths:</b> ' + squadAllDeaths;
document.getElementById('squadAllKd').innerHTML = '<b>squad K/D Rate:</b> ' + squadAllKd;
document.getElementById('squadAllKPM').innerHTML = '<b>Eliminations Per Minute:</b> ' + squadAllKPM;
document.getElementById('squadAllSPM').innerHTML ='<b>Squad Score Per Minute:</b> ' + squadAllSPM;
document.getElementById('squadAllSPM2').innerHTML ='<b>Squad Score Per Match:</b> ' + squadAllSPM2;
document.getElementById('squadAllPlacetop3').innerHTML ='<b>Placed Top 3:</b> '+ squadAllPlacetop3;
document.getElementById('squadAllPlacetop6').innerHTML='<b>Placed top 6:</b> '+ squadAllPlacetop6;
document.getElementById('squadAllPO').innerHTML = '<b>Players Outlasted:</b> ' + squadAllPO;
,
);
$.getJSON(
url: ' https://fortniteapi.io/v1/stats?account=f313b75dc8864c35829c844e1637be4d',
headers: Authorization: '5d661c2a-59e41e68-854c5a46-6f742ef8' ,
success: function (data)
console.log(data)
);
</script>
</body>
</html>
【问题讨论】:
null(实际的空值)和字符串“null”不是一回事。 hmm,那我怎么告诉JS我想要真正的null呢? 不使用字符串?检查 null(一个 null),而不是“null”(一个字符串) 不起作用,即使没有字符串 但你的逻辑似乎倒退了;你基本上说“如果这是空的,那么从它访问一些属性。但它是空的,它不会有任何。 【参考方案1】:请使用缩进...
if(data.data.stats.all.trio == null)
var trioAllMP = data.data.stats.all.trio.minutesPlayed;
var trioAllMatches = data.data.stats.all.trio.matches;
var trioML = Math.round((trioAllMP/trioAllMatches + Number.EPSILON) * 100) / 100 + ' Minutes';
var trioAllWins = data.data.stats.all.trio.wins;
var trioAllWinrate = data.data.stats.all.trio.winRate;
var trioAllKills = data.data.stats.all.trio.kills;
var trioAllDeaths = data.data.stats.all.trio.deaths;
var trioAllKd = data.data.stats.all.trio.kd;
var trioAllKPM = data.data.stats.all.trio.killsPerMin;
var trioAllScore = data.data.stats.all.trio.score;
var trioAllSPM = data.data.stats.all.trio.scorePerMin;
var trioAllSPM2 = data.data.stats.all.trio.scorePerMatch;
var trioAllPlacetop3 = data.data.stats.all.trio.top3;
var trioAllPlacetop6 = data.data.stats.all.trio.top6;
var trioAllPO = data.data.stats.all.trio.playersOutlived;
var trioTime = time_convert(trioAllMP);
document.getElementById('trioAllMP').innerHTML = '<b>Time Played:</b> ' + trioTime;
document.getElementById('trioAllScore').innerHTML = '<b>Trio Score:</b> ' + trioAllScore;
document.getElementById('trioAllMatches').innerHTML = '<b>Number of Matches:</b> '+ trioAllMatches;
document.getElementById('trioMatchLength').innerHTML ='<b>AVG Length of Match:</b> '+ trioML;
document.getElementById('trioAllWins').innerHTML = '<b>Total Wins:</b> ' + trioAllWins;
document.getElementById('trioAllWinrate').innerHTML = '<b>Trio Winrate:</b> ' + trioAllWinrate;
document.getElementById('trioAllKills').innerHTML = '<b>Trio Eliminations:</b> ' + trioAllKills;
document.getElementById('trioAllDeaths').innerHTML = '<b>Trio Deaths:</b> ' + trioAllDeaths;
document.getElementById('trioAllKd').innerHTML = '<b>Trio K/D Rate:</b> ' + trioAllKd;
document.getElementById('trioAllKPM').innerHTML = '<b>Eliminations Per Minute:</b> ' + trioAllKPM;
document.getElementById('trioAllSPM').innerHTML ='<b>Trio Score Per Minute:</b> ' + trioAllSPM;
document.getElementById('trioAllSPM2').innerHTML ='<b>Trio Score Per Match:</b> ' + trioAllSPM2;
document.getElementById('trioAllPlacetop3').innerHTML ='<b>Placed Top 3:</b> '+ trioAllPlacetop3;
document.getElementById('trioAllPlacetop6').innerHTML='<b>Placed top 6:</b> '+ trioAllPlacetop6;
document.getElementById('trioAllPO').innerHTML = '<b>Players Outlasted:</b> ' + trioAllPO;
如果比较相同并且它们彼此相邻,请考虑仅使用一个 if 语句!
if(data.data.stats.all.trio == null)
var trioAllMP = data.data.stats.all.trio.minutesPlayed;
var trioAllMatches = data.data.stats.all.trio.matches;
var trioML = Math.round((trioAllMP/trioAllMatches + Number.EPSILON) * 100) / 100 + ' Minutes';
var trioAllWins = data.data.stats.all.trio.wins;
var trioAllWinrate = data.data.stats.all.trio.winRate;
var trioAllKills = data.data.stats.all.trio.kills;
var trioAllDeaths = data.data.stats.all.trio.deaths;
var trioAllKd = data.data.stats.all.trio.kd;
var trioAllKPM = data.data.stats.all.trio.killsPerMin;
var trioAllScore = data.data.stats.all.trio.score;
var trioAllSPM = data.data.stats.all.trio.scorePerMin;
var trioAllSPM2 = data.data.stats.all.trio.scorePerMatch;
var trioAllPlacetop3 = data.data.stats.all.trio.top3;
var trioAllPlacetop6 = data.data.stats.all.trio.top6;
var trioAllPO = data.data.stats.all.trio.playersOutlived;
var trioTime = time_convert(trioAllMP);
document.getElementById('trioAllMP').innerHTML = '<b>Time Played:</b> ' + trioTime;
document.getElementById('trioAllScore').innerHTML = '<b>Trio Score:</b> ' + trioAllScore;
document.getElementById('trioAllMatches').innerHTML = '<b>Number of Matches:</b> '+ trioAllMatches;
document.getElementById('trioMatchLength').innerHTML ='<b>AVG Length of Match:</b> '+ trioML;
document.getElementById('trioAllWins').innerHTML = '<b>Total Wins:</b> ' + trioAllWins;
document.getElementById('trioAllWinrate').innerHTML = '<b>Trio Winrate:</b> ' + trioAllWinrate;
document.getElementById('trioAllKills').innerHTML = '<b>Trio Eliminations:</b> ' + trioAllKills;
document.getElementById('trioAllDeaths').innerHTML = '<b>Trio Deaths:</b> ' + trioAllDeaths;
document.getElementById('trioAllKd').innerHTML = '<b>Trio K/D Rate:</b> ' + trioAllKd;
document.getElementById('trioAllKPM').innerHTML = '<b>Eliminations Per Minute:</b> ' + trioAllKPM;
document.getElementById('trioAllSPM').innerHTML ='<b>Trio Score Per Minute:</b> ' + trioAllSPM;
document.getElementById('trioAllSPM2').innerHTML ='<b>Trio Score Per Match:</b> ' + trioAllSPM2;
document.getElementById('trioAllPlacetop3').innerHTML ='<b>Placed Top 3:</b> '+ trioAllPlacetop3;
document.getElementById('trioAllPlacetop6').innerHTML='<b>Placed top 6:</b> '+ trioAllPlacetop6;
document.getElementById('trioAllPO').innerHTML = '<b>Players Outlasted:</b> ' + trioAllPO;
$(".trioStats").remove;
更重要的是:
我建议您为您的浏览器安装 JSON Formater 扩展,然后您会更清楚地看到您尝试访问的数据不存在!
并删除“typeof”,因为不需要找到null
【讨论】:
所以我看到缩进是这里的错误,而不是你:| 我建议您为您的浏览器安装 JSON Formater 扩展,然后您会更清楚地看到您尝试访问的数据不存在! wdym? data.data.stats.all.trio 存在 我知道它不存在,我希望它检测到它不存在。 fortnite-api.com/v1/stats/br/v2?name=chefbezos 是一个示例端点,将其更改为 然后你就有数据了。用户输入将控制名称参数以上是关于如果 JSON 中的数据存在语句不起作用的主要内容,如果未能解决你的问题,请参考以下文章