[英]Accessing a JSON object in an array
I've gone through the Lynda courses on JSON and really searched around to solve this bug; 我已经完成了有关JSON的Lynda课程,并且确实四处搜寻以解决此错误; however, I guess my question is too specific and I need an example. 但是,我想我的问题太具体了,我需要一个例子。
JSON JSON格式
{
"date": "2017-10-15",
"league": "NHL",
"odds": "spreads",
"status": "200 OK",
"updated": "2017-10-23 00:07 UTC",
"games": [
{
"away": {
"rot": "051",
"odds": {
"acesport": "-1.5 +178",
"betphoenix": "-1.5 +177",
"betus": "-1.5 +170",
"bookmaker": "-1.5 +180",
"bovada": "-1.5 +170",
"dsi": "-1.5 +170",
"easystreet": "-1.5 +180",
"jazz": "-1.5 +175",
"mirage": "-1.5 +180",
"open": "-1.5 -110",
"pinnacle": "-1.5 +192",
"sbg": "-1.5 +175",
"sia": "-1.5 +150",
"sportsbet": "-1.5 +240",
"station": "-1.5 +175",
"westgate": "-1.5 +175"
},
"team": "Boston Bruins",
"score": 1
},
"home": {
"rot": "052",
"odds": {
"acesport": "+1.5 -208",
"betphoenix": "+1.5 -217",
"betus": "+1.5 -200",
"bookmaker": "+1.5 -210",
"bovada": "+1.5 -200",
"dsi": "+1.5 -200",
"easystreet": "+1.5 -210",
"jazz": "+1.5 -205",
"mirage": "+1.5 -220",
"open": "+1.5 -110",
"pinnacle": "+1.5 -214",
"sbg": "+1.5 -210",
"sia": "+1.5 -175",
"sportsbet": "+1.5 -280",
"station": "+1.5 -210",
"westgate": "+1.5 -200"
},
"team": "Vegas Golden Knights",
"score": 3
},
"time": "7:05 PM EST",
"status": "Final"
}
]
}
From what I've gathered from searching and videos I think the code below is close. 从搜索和视频中收集的信息来看,我认为下面的代码已经接近。 The issue must be that this inst just a value in an array but another object? 问题一定是这个inst只是数组中的值,而是另一个对象?
for (i = 0; i <= body.games.length; i++){
for (var key in body.games[i]) {
console.log("Game " + (i +1));
console.log(body.games[i][key].away.odds.acesport);
}
}
Your post doesn't contain a question. 您的帖子没有问题。 I will assume the questions "How to access an object in an array?" 我将假设以下问题:“如何访问数组中的对象?” and "Why does my code not work?". 和“为什么我的代码不起作用?”。
Maybe this example will clarify it for you. 也许这个例子会为您澄清。
for (var i = 0; i <= body.games.length; i++) {
var game = body.games[i];
console.log("Game number " + (i + 1));
console.log(game.away.odds.acesport);
for (var key in game) {
// This will print the strings "away", "home", "time" and "status",
// but not the values game[key] which are "object", "object",
// "7:05 PM EST" and "Final".
console.log("Game property: " + key);
}
}
You are trying to access body.games[i].away.away.odds.acesport
, this wont work. 您正在尝试访问body.games[i].away.away.odds.acesport
,但此方法body.games[i].away.away.odds.acesport
。 You should try some approaches: 您应该尝试一些方法:
Just get the values of the 2 keys you know have the properties you want. 只需获取您知道具有所需属性的2个键的值即可。
for (i = 0; i <= body.games.length - 1; i++){
console.log("Game " + (i +1));
console.log(body.games[i].away.odds.acesport);
console.log(body.games[i].home.odds.acesport);
}
Check if the property odds and acesport exists. 检查属性赔率和aceport是否存在。 This is more scalable. 这更具可扩展性。
for (i = 0; i <= body.games.length - 1; i++) {
console.log("Game " + (i +1));
for (var key in body.games[i]) {
var acesport = body.games[i][key].odds && body.games[i][key].odds.acesport;
if (acesport) console.log(acesport);
}
}
Now, if you want to try a little cleaner: 现在,如果您想尝试一点清洁剂:
body.games.forEach(function(game, index) {
console.log("Game " + index + 1);
Object.keys(game).forEach(function (prop) {
var acesport = game[prop].odds && game[prop].odds.acesport;
if (acesport) console.log(acesport);
});
});
Or cleaner yet (if you can use string template and arrow functions): 还是更干净(如果可以使用字符串模板和箭头函数):
body.games.forEach((game, index) => {
console.log(`Game ${index + 1}`);
Object.keys(game).forEach((prop) => {
var acesport = game[prop].odds && game[prop].odds.acesport;
if (acesport) console.log(acesport);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.